blob: f8dfe335c946939f47b4b5a03d5c8a9455738729 [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>
25#include <common/memory.h>
26#include <common/standard.h>
27#include <common/version.h>
28
29#include <types/global.h>
30
31#include <proto/arg.h>
32#include <proto/auth.h>
33#include <proto/http_fetch.h>
Christopher Fauletef453ed2018-10-24 21:39:27 +020034#include <proto/http_htx.h>
35#include <proto/htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020036#include <proto/log.h>
37#include <proto/obj_type.h>
38#include <proto/proto_http.h>
39#include <proto/sample.h>
40#include <proto/stream.h>
41
42
43/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
44static THREAD_LOCAL struct hdr_ctx static_hdr_ctx;
Christopher Fauletef453ed2018-10-24 21:39:27 +020045static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
46
Willy Tarreau79e57332018-10-02 16:01:16 +020047
48/*
49 * Returns the data from Authorization header. Function may be called more
50 * than once so data is stored in txn->auth_data. When no header is found
51 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
52 * searching again for something we are unable to find anyway. However, if
53 * the result if valid, the cache is not reused because we would risk to
54 * have the credentials overwritten by another stream in parallel.
55 */
56
Christopher Faulet311c7ea2018-10-24 21:41:55 +020057static int get_http_auth(struct sample *smp)
Willy Tarreau79e57332018-10-02 16:01:16 +020058{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020059 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020060 struct http_txn *txn = s->txn;
61 struct buffer auth_method;
Willy Tarreau79e57332018-10-02 16:01:16 +020062 char *h, *p;
63 int len;
64
65#ifdef DEBUG_AUTH
66 printf("Auth for stream %p: %d\n", s, txn->auth.method);
67#endif
Willy Tarreau79e57332018-10-02 16:01:16 +020068 if (txn->auth.method == HTTP_AUTH_WRONG)
69 return 0;
70
71 txn->auth.method = HTTP_AUTH_WRONG;
72
Christopher Faulet311c7ea2018-10-24 21:41:55 +020073 if (IS_HTX_STRM(s) || (smp->px->mode == PR_MODE_TCP)) {
74 /* HTX version */
75 struct htx *htx = htx_from_buf(&s->req.buf);
76 struct http_hdr_ctx ctx = { .blk = NULL };
77 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020078
Christopher Faulet311c7ea2018-10-24 21:41:55 +020079 if (txn->flags & TX_USE_PX_CONN)
80 hdr = ist("Proxy-Authorization");
81 else
82 hdr = ist("Authorization");
83
84 htx = htx_from_buf(&s->req.buf);
85 ctx.blk = NULL;
86 if (!http_find_header(htx, hdr, &ctx, 0))
87 return 0;
88
89 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
90 len = p - ctx.value.ptr;
91 if (!p || len <= 0)
92 return 0;
93
94 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
95 return 0;
96
97 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +020098 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +020099 else {
100 /* LEGACY version */
101 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau79e57332018-10-02 16:01:16 +0200102
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200103 if (txn->flags & TX_USE_PX_CONN) {
104 h = "Proxy-Authorization";
105 len = strlen(h);
106 } else {
107 h = "Authorization";
108 len = strlen(h);
109 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200110
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200111 if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx))
112 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200113
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200114 h = ctx.line + ctx.val;
Willy Tarreau79e57332018-10-02 16:01:16 +0200115
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200116 p = memchr(h, ' ', ctx.vlen);
117 len = p - h;
118 if (!p || len <= 0)
119 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200120
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200121 if (chunk_initlen(&auth_method, h, 0, len) != 1)
122 return 0;
123
124 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
125 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200126
127 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
128 struct buffer *http_auth = get_trash_chunk();
129
130 len = base64dec(txn->auth.method_data.area,
131 txn->auth.method_data.data,
132 http_auth->area, global.tune.bufsize - 1);
133
134 if (len < 0)
135 return 0;
136
137
138 http_auth->area[len] = '\0';
139
140 p = strchr(http_auth->area, ':');
141
142 if (!p)
143 return 0;
144
145 txn->auth.user = http_auth->area;
146 *p = '\0';
147 txn->auth.pass = p+1;
148
149 txn->auth.method = HTTP_AUTH_BASIC;
150 return 1;
151 }
152
153 return 0;
154}
155
156/* This function ensures that the prerequisites for an L7 fetch are ready,
157 * which means that a request or response is ready. If some data is missing,
158 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Fauletef453ed2018-10-24 21:39:27 +0200159 * to extract data from L7.
160 *
161 * The function returns :
162 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
163 * decide whether or not an HTTP message is present ;
164 * NULL if the requested data cannot be fetched or if it is certain that
165 * we'll never have any HTTP message there ;
166 * The HTX message if ready
167 */
168struct htx *smp_prefetch_htx(struct sample *smp, const struct arg *args)
169{
170 struct proxy *px = smp->px;
171 struct stream *s = smp->strm;
172 unsigned int opt = smp->opt;
173 struct http_txn *txn = NULL;
174 struct htx *htx = NULL;
175
176 /* Note: it is possible that <s> is NULL when called before stream
177 * initialization (eg: tcp-request connection), so this function is the
178 * one responsible for guarding against this case for all HTTP users.
179 */
180 if (!s)
181 return NULL;
182
183 if (!s->txn) {
184 if (unlikely(!http_alloc_txn(s)))
185 return NULL; /* not enough memory */
186 http_init_txn(s);
187 txn = s->txn;
188 }
189
190 if (px->mode == PR_MODE_HTTP) {
191 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
192 union h1_sl sl;
193
194 htx = htx_from_buf(&s->req.buf);
195 if (htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH) {
196 /* Parsing is done by the mux, just wait */
197 smp->flags |= SMP_F_MAY_CHANGE;
198 return NULL;
199 }
200
201 /* OK we just got a valid HTTP request. We have some
202 * minor preparation to perform so that further checks
203 * can rely on HTTP tests.
204 */
205 if (txn) {
206 sl = http_find_stline(htx);
207 txn->meth = sl.rq.meth;
208 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
209 s->flags |= SF_REDIRECTABLE;
210 }
211
212 /* otherwise everything's ready for the request */
213 }
214 else {
215 htx = htx_from_buf(&s->res.buf);
216 if (htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH) {
217 /* Parsing is done by the mux, just wait */
218 smp->flags |= SMP_F_MAY_CHANGE;
219 return NULL;
220 }
221 }
222 }
223 else { /* PR_MODE_TCP */
224 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
225 struct buffer *buf;
226 struct h1m h1m;
227 struct http_hdr hdrs[MAX_HTTP_HDR];
228 union h1_sl sl;
229 int ret;
230
231 buf = &s->req.buf;
232 if (b_head(buf) + b_data(buf) > b_wrap(buf))
233 b_slow_realign(buf, trash.area, 0);
234
235 h1m_init_req(&h1m);
236 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
237 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &sl);
238 if (ret <= 0) {
239 /* Invalid or too big*/
240 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
241 return NULL;
242
243 /* wait for a full request */
244 smp->flags |= SMP_F_MAY_CHANGE;
245 return NULL;
246 }
247
248 /* OK we just got a valid HTTP request. We have to
249 * convert it into an HTX message.
250 */
251 if (unlikely(sl.rq.v.len == 0)) {
252 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
253 if (sl.rq.meth != HTTP_METH_GET || !sl.rq.u.len)
254 return NULL;
255 sl.rq.v = ist("HTTP/1.0");
256 }
257 htx = htx_from_buf(get_trash_chunk());
258 if (!htx_add_reqline(htx, sl) || !htx_add_all_headers(htx, hdrs))
259 return NULL;
260
261 if (txn) {
262 txn->meth = sl.rq.meth;
263 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
264 s->flags |= SF_REDIRECTABLE;
265 }
266 /* Ok, now everything's ready for the request */
267 }
268 else {
269 /* Impossible, no HTTP fetch on tcp-response */
270 return NULL;
271 }
272 }
273
274 /* everything's OK */
275 smp->data.u.sint = 1;
276 return htx;
277}
278
279/* This function ensures that the prerequisites for an L7 fetch are ready,
280 * which means that a request or response is ready. If some data is missing,
281 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau79e57332018-10-02 16:01:16 +0200282 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
283 * another test is made to ensure the required information is not gone.
284 *
285 * The function returns :
286 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
287 * decide whether or not an HTTP message is present ;
288 * 0 if the requested data cannot be fetched or if it is certain that
289 * we'll never have any HTTP message there ;
290 * 1 if an HTTP message is ready
291 */
292int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
293 const struct arg *args, struct sample *smp, int req_vol)
294{
295 struct http_txn *txn;
296 struct http_msg *msg;
297
298 /* Note: it is possible that <s> is NULL when called before stream
299 * initialization (eg: tcp-request connection), so this function is the
300 * one responsible for guarding against this case for all HTTP users.
301 */
302 if (!s)
303 return 0;
304
305 if (!s->txn) {
306 if (unlikely(!http_alloc_txn(s)))
307 return 0; /* not enough memory */
308 http_init_txn(s);
309 }
310 txn = s->txn;
311 msg = &txn->req;
312
313 /* Check for a dependency on a request */
314 smp->data.type = SMP_T_BOOL;
315
316 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
317 /* If the buffer does not leave enough free space at the end,
318 * we must first realign it.
319 */
320 if (ci_head(&s->req) > b_orig(&s->req.buf) &&
321 ci_head(&s->req) + ci_data(&s->req) > b_wrap(&s->req.buf) - global.tune.maxrewrite)
322 channel_slow_realign(&s->req, trash.area);
323
324 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
325 if (msg->msg_state == HTTP_MSG_ERROR)
326 return 0;
327
328 /* Try to decode HTTP request */
329 if (likely(msg->next < ci_data(&s->req)))
330 http_msg_analyzer(msg, &txn->hdr_idx);
331
332 /* Still no valid request ? */
333 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
334 if ((msg->msg_state == HTTP_MSG_ERROR) ||
335 channel_full(&s->req, global.tune.maxrewrite)) {
336 return 0;
337 }
338 /* wait for final state */
339 smp->flags |= SMP_F_MAY_CHANGE;
340 return 0;
341 }
342
343 /* OK we just got a valid HTTP request. We have some minor
344 * preparation to perform so that further checks can rely
345 * on HTTP tests.
346 */
347
348 /* If the request was parsed but was too large, we must absolutely
349 * return an error so that it is not processed. At the moment this
350 * cannot happen, but if the parsers are to change in the future,
351 * we want this check to be maintained.
352 */
353 if (unlikely(ci_head(&s->req) + ci_data(&s->req) >
354 b_wrap(&s->req.buf) - global.tune.maxrewrite)) {
355 msg->err_state = msg->msg_state;
356 msg->msg_state = HTTP_MSG_ERROR;
357 smp->data.u.sint = 1;
358 return 1;
359 }
360
361 txn->meth = find_http_meth(ci_head(msg->chn), msg->sl.rq.m_l);
362 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
363 s->flags |= SF_REDIRECTABLE;
364
365 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
366 return 0;
367 }
368
369 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
370 return 0; /* data might have moved and indexes changed */
371 }
372
373 /* otherwise everything's ready for the request */
374 }
375 else {
376 /* Check for a dependency on a response */
377 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
378 smp->flags |= SMP_F_MAY_CHANGE;
379 return 0;
380 }
381 }
382
383 /* everything's OK */
384 smp->data.u.sint = 1;
385 return 1;
386}
387
388/* This function fetches the method of current HTTP request and stores
389 * it in the global pattern struct as a chunk. There are two possibilities :
390 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
391 * in <len> and <ptr> is NULL ;
392 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
393 * <len> to its length.
394 * This is intended to be used with pat_match_meth() only.
395 */
396static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
397{
398 int meth;
399 struct http_txn *txn;
400
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200401 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
402 /* HTX version */
403 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +0200404
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200405 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +0200406 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200407
408 txn = smp->strm->txn;
409 meth = txn->meth;
410 smp->data.type = SMP_T_METH;
411 smp->data.u.meth.meth = meth;
412 if (meth == HTTP_METH_OTHER) {
413 union h1_sl sl;
414
415 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
416 /* ensure the indexes are not affected */
417 return 0;
418
419 sl = http_find_stline(htx);
420 smp->flags |= SMP_F_CONST;
421 smp->data.u.meth.str.area = sl.rq.m.ptr;
422 smp->data.u.meth.str.data = sl.rq.m.len;
423 }
424 smp->flags |= SMP_F_VOL_1ST;
425 }
426 else {
427 /* LEGACY version */
428 CHECK_HTTP_MESSAGE_FIRST_PERM();
429
430 txn = smp->strm->txn;
431 meth = txn->meth;
432 smp->data.type = SMP_T_METH;
433 smp->data.u.meth.meth = meth;
434 if (meth == HTTP_METH_OTHER) {
435 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
436 /* ensure the indexes are not affected */
437 return 0;
438 smp->flags |= SMP_F_CONST;
439 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
440 smp->data.u.meth.str.area = ci_head(txn->req.chn);
441 }
442 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200443 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200444 return 1;
445}
446
447static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
448{
449 struct http_txn *txn;
450 char *ptr;
451 int len;
452
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200453 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
454 /* HTX version */
455 struct htx *htx = smp_prefetch_htx(smp, args);
456 union h1_sl sl;
457
458 if (!htx)
459 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200460
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200461 sl = http_find_stline(htx);
462 len = sl.rq.v.len;
463 ptr = sl.rq.v.ptr;
464 }
465 else {
466 /* LEGACY version */
467 CHECK_HTTP_MESSAGE_FIRST();
468
469 txn = smp->strm->txn;
470 len = txn->req.sl.rq.v_l;
471 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.v;
472 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200473
474 while ((len-- > 0) && (*ptr++ != '/'));
475 if (len <= 0)
476 return 0;
477
478 smp->data.type = SMP_T_STR;
479 smp->data.u.str.area = ptr;
480 smp->data.u.str.data = len;
481
482 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
483 return 1;
484}
485
486static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
487{
488 struct http_txn *txn;
489 char *ptr;
490 int len;
491
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200492 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
493 /* HTX version */
494 struct htx *htx = smp_prefetch_htx(smp, args);
495 union h1_sl sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200496
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200497 if (!htx)
498 return 0;
499
500 sl = http_find_stline(htx);
501 len = sl.st.v.len;
502 ptr = sl.st.v.ptr;
503 }
504 else {
505 /* LEGACY version */
506 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +0200507
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200508 txn = smp->strm->txn;
509 if (txn->rsp.msg_state < HTTP_MSG_BODY)
510 return 0;
511
512 len = txn->rsp.sl.st.v_l;
513 ptr = ci_head(txn->rsp.chn);
514 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200515
516 while ((len-- > 0) && (*ptr++ != '/'));
517 if (len <= 0)
518 return 0;
519
520 smp->data.type = SMP_T_STR;
521 smp->data.u.str.area = ptr;
522 smp->data.u.str.data = len;
523
524 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
525 return 1;
526}
527
528/* 3. Check on Status Code. We manipulate integers here. */
529static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
530{
531 struct http_txn *txn;
532 char *ptr;
533 int len;
534
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200535 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
536 /* HTX version */
537 struct htx *htx = smp_prefetch_htx(smp, args);
538 union h1_sl sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200539
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200540 if (!htx)
541 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200542
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200543 sl = http_find_stline(htx);
544 len = sl.st.c.len;
545 ptr = sl.st.c.ptr;
546 }
547 else {
548 /* LEGACY version */
549 CHECK_HTTP_MESSAGE_FIRST();
550
551 txn = smp->strm->txn;
552 if (txn->rsp.msg_state < HTTP_MSG_BODY)
553 return 0;
554
555 len = txn->rsp.sl.st.c_l;
556 ptr = ci_head(txn->rsp.chn) + txn->rsp.sl.st.c;
557 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200558
559 smp->data.type = SMP_T_SINT;
560 smp->data.u.sint = __strl2ui(ptr, len);
561 smp->flags = SMP_F_VOL_1ST;
562 return 1;
563}
564
565static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
566{
567 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
568 return 0;
569
570 if (!smp->strm->unique_id) {
571 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
572 return 0;
573 smp->strm->unique_id[0] = '\0';
574 }
575 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
576 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
577
578 smp->data.type = SMP_T_STR;
579 smp->data.u.str.area = smp->strm->unique_id;
580 smp->flags = SMP_F_CONST;
581 return 1;
582}
583
584/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800585 * empty line which separes headers from the body. This is useful
586 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200587 */
588static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
589{
Willy Tarreau79e57332018-10-02 16:01:16 +0200590 struct http_txn *txn;
591
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200592 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
593 /* HTX version */
594 struct htx *htx = smp_prefetch_htx(smp, args);
595 struct buffer *temp;
596 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200597
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200598 if (!htx)
599 return 0;
600 temp = get_trash_chunk();
601 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
602 struct htx_blk *blk = htx_get_blk(htx, pos);
603 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200604
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200605 if (type == HTX_BLK_HDR) {
606 struct ist n = htx_get_blk_name(htx, blk);
607 struct ist v = htx_get_blk_value(htx, blk);
608
609 if (!htx_hdr_to_str(n, v, temp))
610 return 0;
611 }
612 else if (type == HTX_BLK_EOH) {
613 if (!chunk_memcat(temp, "\r\n", 2))
614 return 0;
615 break;
616 }
617 }
618 smp->data.type = SMP_T_STR;
619 smp->data.u.str = *temp;
620
621 }
622 else {
623 /* LEGACY version */
624 struct http_msg *msg;
625 struct hdr_idx *idx;
626
627 CHECK_HTTP_MESSAGE_FIRST();
628
629 txn = smp->strm->txn;
630 idx = &txn->hdr_idx;
631 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200632
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200633 smp->data.type = SMP_T_STR;
634 smp->data.u.str.area = ci_head(msg->chn) + hdr_idx_first_pos(idx);
635 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
636 (ci_head(msg->chn)[msg->eoh] == '\r');
637 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200638 return 1;
639}
640
641/* Returns the header request in a length/value encoded format.
642 * This is useful for exchanges with the SPOE.
643 *
644 * A "length value" is a multibyte code encoding numbers. It uses the
645 * SPOE format. The encoding is the following:
646 *
647 * Each couple "header name" / "header value" is composed
648 * like this:
649 * "length value" "header name bytes"
650 * "length value" "header value bytes"
651 * When the last header is reached, the header name and the header
652 * value are empty. Their length are 0
653 */
654static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
655{
Willy Tarreau79e57332018-10-02 16:01:16 +0200656 struct http_txn *txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200657 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200658
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200659 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
660 /* HTX version */
661 struct htx *htx = smp_prefetch_htx(smp, args);
662 struct buffer *temp;
663 char *p, *end;
664 int32_t pos;
665 int ret;
Willy Tarreau79e57332018-10-02 16:01:16 +0200666
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200667 if (!htx)
668 return 0;
669 temp = get_trash_chunk();
670 p = temp->area;
671 end = temp->area + temp->size;
672 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
673 struct htx_blk *blk = htx_get_blk(htx, pos);
674 enum htx_blk_type type = htx_get_blk_type(blk);
675 struct ist n, v;
Willy Tarreau79e57332018-10-02 16:01:16 +0200676
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200677 if (type == HTX_BLK_HDR) {
678 n = htx_get_blk_name(htx,blk);
679 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200680
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200681 /* encode the header name. */
682 ret = encode_varint(n.len, &p, end);
683 if (ret == -1)
684 return 0;
685 if (p + n.len > end)
686 return 0;
687 memcpy(p, n.ptr, n.len);
688 p += n.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200689
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200690 /* encode the header value. */
691 ret = encode_varint(v.len, &p, end);
692 if (ret == -1)
693 return 0;
694 if (p + v.len > end)
695 return 0;
696 memcpy(p, v.ptr, v.len);
697 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200698
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200699 }
700 else if (type == HTX_BLK_EOH) {
701 /* encode the end of the header list with empty
702 * header name and header value.
703 */
704 ret = encode_varint(0, &p, end);
705 if (ret == -1)
706 return 0;
707 ret = encode_varint(0, &p, end);
708 if (ret == -1)
709 return 0;
710 break;
711 }
712 }
713
714 /* Initialise sample data which will be filled. */
715 smp->data.type = SMP_T_BIN;
716 smp->data.u.str.area = temp->area;
717 smp->data.u.str.data = p - temp->area;
718 smp->data.u.str.size = temp->size;
719 }
720 else {
721 /* LEGACY version */
722 struct http_msg *msg;
723 struct hdr_idx *idx;
724 const char *cur_ptr, *cur_next, *p;
725 int old_idx, cur_idx;
726 struct hdr_idx_elem *cur_hdr;
727 const char *hn, *hv;
728 int hnl, hvl;
729 int ret;
730 char *buf;
731 char *end;
732
733 CHECK_HTTP_MESSAGE_FIRST();
734
735 temp = get_trash_chunk();
736 buf = temp->area;
737 end = temp->area + temp->size;
738
739 txn = smp->strm->txn;
740 idx = &txn->hdr_idx;
741 msg = &txn->req;
742
743 /* Build array of headers. */
744 old_idx = 0;
745 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(idx);
746 while (1) {
747 cur_idx = idx->v[old_idx].next;
748 if (!cur_idx)
749 break;
750 old_idx = cur_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +0200751
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200752 cur_hdr = &idx->v[cur_idx];
753 cur_ptr = cur_next;
754 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
755
756 /* Now we have one full header at cur_ptr of len cur_hdr->len,
757 * and the next header starts at cur_next. We'll check
758 * this header in the list as well as against the default
759 * rule.
760 */
761
762 /* look for ': *'. */
763 hn = cur_ptr;
764 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
765 if (p >= cur_ptr+cur_hdr->len)
766 continue;
767 hnl = p - hn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200768 p++;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200769 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
770 p++;
771 if (p >= cur_ptr + cur_hdr->len)
772 continue;
773 hv = p;
774 hvl = cur_ptr + cur_hdr->len-p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200775
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200776 /* encode the header name. */
777 ret = encode_varint(hnl, &buf, end);
778 if (ret == -1)
779 return 0;
780 if (buf + hnl > end)
781 return 0;
782 memcpy(buf, hn, hnl);
783 buf += hnl;
784
785 /* encode and copy the value. */
786 ret = encode_varint(hvl, &buf, end);
787 if (ret == -1)
788 return 0;
789 if (buf + hvl > end)
790 return 0;
791 memcpy(buf, hv, hvl);
792 buf += hvl;
793 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200794
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200795 /* encode the end of the header list with empty
796 * header name and header value.
797 */
798 ret = encode_varint(0, &buf, end);
Willy Tarreau79e57332018-10-02 16:01:16 +0200799 if (ret == -1)
800 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200801 ret = encode_varint(0, &buf, end);
802 if (ret == -1)
Willy Tarreau79e57332018-10-02 16:01:16 +0200803 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200804
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200805 /* Initialise sample data which will be filled. */
806 smp->data.type = SMP_T_BIN;
807 smp->data.u.str.area = temp->area;
808 smp->data.u.str.data = buf - temp->area;
809 smp->data.u.str.size = temp->size;
810 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200811 return 1;
812}
813
814/* returns the longest available part of the body. This requires that the body
815 * has been waited for using http-buffer-request.
816 */
817static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
818{
Willy Tarreau79e57332018-10-02 16:01:16 +0200819 struct buffer *temp;
820
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200821 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
822 /* HTX version */
823 struct htx *htx = smp_prefetch_htx(smp, args);
824 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200825
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200826 if (!htx)
827 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200828
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200829 temp = get_trash_chunk();
830 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
831 struct htx_blk *blk = htx_get_blk(htx, pos);
832 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200833
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200834 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
835 break;
836 if (type == HTX_BLK_DATA) {
837 if (!htx_data_to_str(htx_get_blk_value(htx, blk), temp, 0))
838 return 0;
839 }
840 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200841
Willy Tarreau79e57332018-10-02 16:01:16 +0200842 smp->data.type = SMP_T_BIN;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200843 smp->data.u.str = *temp;
844 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200845 }
846 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200847 /* LEGACY version */
848 struct http_msg *msg;
849 unsigned long len;
850 unsigned long block1;
851 char *body;
852
853 CHECK_HTTP_MESSAGE_FIRST();
854
855 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
856 msg = &smp->strm->txn->req;
857 else
858 msg = &smp->strm->txn->rsp;
859
860 len = http_body_bytes(msg);
861 body = c_ptr(msg->chn, -http_data_rewind(msg));
862
863 block1 = len;
864 if (block1 > b_wrap(&msg->chn->buf) - body)
865 block1 = b_wrap(&msg->chn->buf) - body;
866
867 if (block1 == len) {
868 /* buffer is not wrapped (or empty) */
869 smp->data.type = SMP_T_BIN;
870 smp->data.u.str.area = body;
871 smp->data.u.str.data = len;
872 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
873 }
874 else {
875 /* buffer is wrapped, we need to defragment it */
876 temp = get_trash_chunk();
877 memcpy(temp->area, body, block1);
878 memcpy(temp->area + block1, b_orig(&msg->chn->buf),
879 len - block1);
880 smp->data.type = SMP_T_BIN;
881 smp->data.u.str.area = temp->area;
882 smp->data.u.str.data = len;
883 smp->flags = SMP_F_VOL_TEST;
884 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200885 }
886 return 1;
887}
888
889
890/* returns the available length of the body. This requires that the body
891 * has been waited for using http-buffer-request.
892 */
893static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
894{
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200895 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
896 /* HTX version */
897 return 0; /* TODO: to be implemented */
898 }
899 else {
900 /* LEGACY version */
901 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200902
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200903 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +0200904
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200905 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
906 msg = &smp->strm->txn->req;
907 else
908 msg = &smp->strm->txn->rsp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200909
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200910 smp->data.type = SMP_T_SINT;
911 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreau79e57332018-10-02 16:01:16 +0200912
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200913 smp->flags = SMP_F_VOL_TEST;
914 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200915 return 1;
916}
917
918
919/* returns the advertised length of the body, or the advertised size of the
920 * chunks available in the buffer. This requires that the body has been waited
921 * for using http-buffer-request.
922 */
923static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
924{
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200925 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
926 /* HTX version */
927 return 0; /* TODO: to be implemented */
928 }
929 else {
930 /* LEGACY version */
931 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200932
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200933 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +0200934
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200935 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
936 msg = &smp->strm->txn->req;
937 else
938 msg = &smp->strm->txn->rsp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200939
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200940 smp->data.type = SMP_T_SINT;
941 smp->data.u.sint = msg->body_len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200942
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200943 smp->flags = SMP_F_VOL_TEST;
944 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200945 return 1;
946}
947
948
949/* 4. Check on URL/URI. A pointer to the URI is stored. */
950static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
951{
952 struct http_txn *txn;
953
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200954 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
955 /* HTX version */
956 struct htx *htx = smp_prefetch_htx(smp, args);
957 union h1_sl sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200958
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200959 if (!htx)
960 return 0;
961 sl = http_find_stline(htx);
962 smp->data.type = SMP_T_STR;
963 smp->data.u.str.area = sl.rq.u.ptr;
964 smp->data.u.str.data = sl.rq.u.len;
965 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
966 }
967 else {
968 /* LEGACY version */
969 CHECK_HTTP_MESSAGE_FIRST();
970
971 txn = smp->strm->txn;
972 smp->data.type = SMP_T_STR;
973 smp->data.u.str.data = txn->req.sl.rq.u_l;
974 smp->data.u.str.area = ci_head(txn->req.chn) + txn->req.sl.rq.u;
975 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
976 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200977 return 1;
978}
979
980static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
981{
982 struct http_txn *txn;
983 struct sockaddr_storage addr;
984
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200985 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
986 /* HTX version */
987 struct htx *htx = smp_prefetch_htx(smp, args);
988 union h1_sl sl;
989
990 if (!htx)
991 return 0;
992 sl = http_find_stline(htx);
993 url2sa(sl.rq.u.ptr, sl.rq.u.len, &addr, NULL);
994 }
995 else {
996 /* LEGACY version */
997 CHECK_HTTP_MESSAGE_FIRST();
998
999 txn = smp->strm->txn;
1000 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
1001 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001002
Willy Tarreau79e57332018-10-02 16:01:16 +02001003 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1004 return 0;
1005
1006 smp->data.type = SMP_T_IPV4;
1007 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
1008 smp->flags = 0;
1009 return 1;
1010}
1011
1012static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
1013{
1014 struct http_txn *txn;
1015 struct sockaddr_storage addr;
1016
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001017 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1018 /* HTX version */
1019 struct htx *htx = smp_prefetch_htx(smp, args);
1020 union h1_sl sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001021
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001022 if (!htx)
1023 return 0;
1024 sl = http_find_stline(htx);
1025 url2sa(sl.rq.u.ptr, sl.rq.u.len, &addr, NULL);
1026 }
1027 else {
1028 /* LEGACY version */
1029 CHECK_HTTP_MESSAGE_FIRST();
1030
1031 txn = smp->strm->txn;
1032 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
1033 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001034 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1035 return 0;
1036
1037 smp->data.type = SMP_T_SINT;
1038 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
1039 smp->flags = 0;
1040 return 1;
1041}
1042
1043/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1044 * Accepts an optional argument of type string containing the header field name,
1045 * and an optional argument of type signed or unsigned integer to request an
1046 * explicit occurrence of the header. Note that in the event of a missing name,
1047 * headers are considered from the first one. It does not stop on commas and
1048 * returns full lines instead (useful for User-Agent or Date for example).
1049 */
1050static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1051{
Willy Tarreau79e57332018-10-02 16:01:16 +02001052 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001053
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001054 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1055 /* HTX version */
1056 struct htx *htx = smp_prefetch_htx(smp, args);
1057 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1058 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +02001059
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001060 if (!ctx) {
1061 /* first call */
1062 ctx = &static_http_hdr_ctx;
1063 ctx->blk = NULL;
1064 smp->ctx.a[0] = ctx;
1065 }
1066
1067 if (args) {
1068 if (args[0].type != ARGT_STR)
1069 return 0;
1070 name.ptr = args[0].data.str.area;
1071 name.len = args[0].data.str.data;
1072
1073 if (args[1].type == ARGT_SINT)
1074 occ = args[1].data.sint;
1075 }
1076
1077 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001078 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001079
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001080 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1081 /* search for header from the beginning */
1082 ctx->blk = NULL;
1083
1084 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1085 /* no explicit occurrence and single fetch => last header by default */
1086 occ = -1;
1087
1088 if (!occ)
1089 /* prepare to report multiple occurrences for ACL fetches */
1090 smp->flags |= SMP_F_NOT_LAST;
1091
1092 smp->data.type = SMP_T_STR;
1093 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1094 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1095 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001096 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001097 else {
1098 /* LEGACY version */
1099 struct hdr_idx *idx;
1100 struct hdr_ctx *ctx = smp->ctx.a[0];
1101 const struct http_msg *msg;
1102 const char *name_str = NULL;
1103 int name_len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001104
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001105 if (!ctx) {
1106 /* first call */
1107 ctx = &static_hdr_ctx;
1108 ctx->idx = 0;
1109 smp->ctx.a[0] = ctx;
1110 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001111
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001112 if (args) {
1113 if (args[0].type != ARGT_STR)
1114 return 0;
1115 name_str = args[0].data.str.area;
1116 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001117
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001118 if (args[1].type == ARGT_SINT)
1119 occ = args[1].data.sint;
1120 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001121
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001122 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001123
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001124 idx = &smp->strm->txn->hdr_idx;
1125 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 +02001126
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001127 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1128 /* search for header from the beginning */
1129 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001130
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001131 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1132 /* no explicit occurrence and single fetch => last header by default */
1133 occ = -1;
1134
1135 if (!occ)
1136 /* prepare to report multiple occurrences for ACL fetches */
1137 smp->flags |= SMP_F_NOT_LAST;
1138
1139 smp->data.type = SMP_T_STR;
1140 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1141 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1142 return 1;
1143 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001144 smp->flags &= ~SMP_F_NOT_LAST;
1145 return 0;
1146}
1147
1148/* 6. Check on HTTP header count. The number of occurrences is returned.
1149 * Accepts exactly 1 argument of type string. It does not stop on commas and
1150 * returns full lines instead (useful for User-Agent or Date for example).
1151 */
1152static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1153{
Willy Tarreau79e57332018-10-02 16:01:16 +02001154 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001155
1156 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1157 /* HTX version */
1158 struct htx *htx = smp_prefetch_htx(smp, args);
1159 struct http_hdr_ctx ctx;
1160 struct ist name;
1161
1162 if (!htx)
1163 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001164
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001165 if (args && args->type == ARGT_STR) {
1166 name.ptr = args->data.str.area;
1167 name.len = args->data.str.data;
1168 }
1169
1170 ctx.blk = NULL;
1171 cnt = 0;
1172 while (http_find_header(htx, name, &ctx, 1))
1173 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001174 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001175 else {
1176 /* LEGACY version */
1177 struct hdr_idx *idx;
1178 struct hdr_ctx ctx;
1179 const struct http_msg *msg;
1180 const char *name = NULL;
1181 int len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001182
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001183 if (args && args->type == ARGT_STR) {
1184 name = args->data.str.area;
1185 len = args->data.str.data;
1186 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001187
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001188 CHECK_HTTP_MESSAGE_FIRST();
1189
1190 idx = &smp->strm->txn->hdr_idx;
1191 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001192
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001193 ctx.idx = 0;
1194 cnt = 0;
1195 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
1196 cnt++;
1197 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001198
1199 smp->data.type = SMP_T_SINT;
1200 smp->data.u.sint = cnt;
1201 smp->flags = SMP_F_VOL_HDR;
1202 return 1;
1203}
1204
1205static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
1206{
Willy Tarreau79e57332018-10-02 16:01:16 +02001207 struct buffer *temp;
1208 char del = ',';
1209
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001210 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1211 /* HTX version */
1212 struct htx *htx = smp_prefetch_htx(smp, args);
1213 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001214
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001215 if (!htx)
1216 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001217
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001218 if (args && args->type == ARGT_STR)
1219 del = *args[0].data.str.area;
Willy Tarreau79e57332018-10-02 16:01:16 +02001220
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001221 temp = get_trash_chunk();
1222 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1223 struct htx_blk *blk = htx_get_blk(htx, pos);
1224 enum htx_blk_type type = htx_get_blk_type(blk);
1225 struct ist n;
Willy Tarreau79e57332018-10-02 16:01:16 +02001226
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001227 if (type == HTX_BLK_EOH)
1228 break;
1229 if (type != HTX_BLK_HDR)
1230 continue;
1231 n = htx_get_blk_name(htx, blk);
1232
1233 if (temp->data)
1234 temp->area[temp->data++] = del;
1235 chunk_memcat(temp, n.ptr, n.len);
1236 }
1237 }
1238 else {
1239 /* LEGACY version */
1240 struct hdr_idx *idx;
1241 struct hdr_ctx ctx;
1242 const struct http_msg *msg;
1243
1244 if (args && args->type == ARGT_STR)
1245 del = *args[0].data.str.area;
1246
1247 CHECK_HTTP_MESSAGE_FIRST();
1248
1249 idx = &smp->strm->txn->hdr_idx;
1250 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
1251
1252 temp = get_trash_chunk();
1253
1254 ctx.idx = 0;
1255 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
1256 if (temp->data)
1257 temp->area[temp->data++] = del;
1258 memcpy(temp->area + temp->data, ctx.line, ctx.del);
1259 temp->data += ctx.del;
1260 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001261 }
1262
1263 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001264 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001265 smp->flags = SMP_F_VOL_HDR;
1266 return 1;
1267}
1268
1269/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1270 * Accepts an optional argument of type string containing the header field name,
1271 * and an optional argument of type signed or unsigned integer to request an
1272 * explicit occurrence of the header. Note that in the event of a missing name,
1273 * headers are considered from the first one.
1274 */
1275static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1276{
Willy Tarreau79e57332018-10-02 16:01:16 +02001277 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001278
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001279 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1280 /* HTX version */
1281 struct htx *htx = smp_prefetch_htx(smp, args);
1282 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1283 struct ist name;
1284
1285 if (!ctx) {
1286 /* first call */
1287 ctx = &static_http_hdr_ctx;
1288 ctx->blk = NULL;
1289 smp->ctx.a[0] = ctx;
1290 }
1291
1292 if (args) {
1293 if (args[0].type != ARGT_STR)
1294 return 0;
1295 name.ptr = args[0].data.str.area;
1296 name.len = args[0].data.str.data;
1297
1298 if (args[1].type == ARGT_SINT)
1299 occ = args[1].data.sint;
1300 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001301
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001302 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001303 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001304
1305 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1306 /* search for header from the beginning */
1307 ctx->blk = NULL;
1308
1309 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1310 /* no explicit occurrence and single fetch => last header by default */
1311 occ = -1;
1312
1313 if (!occ)
1314 /* prepare to report multiple occurrences for ACL fetches */
1315 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001316
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001317 smp->data.type = SMP_T_STR;
1318 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1319 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1320 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001321 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001322 else {
1323 /* LEGACY version */
1324 struct hdr_idx *idx;
1325 struct hdr_ctx *ctx = smp->ctx.a[0];
1326 const struct http_msg *msg;
1327 const char *name_str = NULL;
1328 int name_len = 0;
1329
1330 if (!ctx) {
1331 /* first call */
1332 ctx = &static_hdr_ctx;
1333 ctx->idx = 0;
1334 smp->ctx.a[0] = ctx;
1335 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001336
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001337 if (args) {
1338 if (args[0].type != ARGT_STR)
1339 return 0;
1340 name_str = args[0].data.str.area;
1341 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001342
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001343 if (args[1].type == ARGT_SINT)
1344 occ = args[1].data.sint;
1345 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001346
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001347 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001348
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001349 idx = &smp->strm->txn->hdr_idx;
1350 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 +02001351
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001352 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1353 /* search for header from the beginning */
1354 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001355
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001356 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1357 /* no explicit occurrence and single fetch => last header by default */
1358 occ = -1;
1359
1360 if (!occ)
1361 /* prepare to report multiple occurrences for ACL fetches */
1362 smp->flags |= SMP_F_NOT_LAST;
1363
1364 smp->data.type = SMP_T_STR;
1365 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1366 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1367 return 1;
1368 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001369
1370 smp->flags &= ~SMP_F_NOT_LAST;
1371 return 0;
1372}
1373
1374/* 6. Check on HTTP header count. The number of occurrences is returned.
1375 * Accepts exactly 1 argument of type string.
1376 */
1377static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1378{
Willy Tarreau79e57332018-10-02 16:01:16 +02001379 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001380
1381 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1382 /* HTX version */
1383 struct htx *htx = smp_prefetch_htx(smp, args);
1384 struct http_hdr_ctx ctx;
1385 struct ist name;
1386
1387 if (!htx)
1388 return 0;
1389
1390 if (args && args->type == ARGT_STR) {
1391 name.ptr = args->data.str.area;
1392 name.len = args->data.str.data;
1393 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001394
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001395 ctx.blk = NULL;
1396 cnt = 0;
1397 while (http_find_header(htx, name, &ctx, 0))
1398 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001399 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001400 else {
1401 /* LEGACY version */
1402 struct hdr_idx *idx;
1403 struct hdr_ctx ctx;
1404 const struct http_msg *msg;
1405 const char *name = NULL;
1406 int len = 0;
1407
1408 if (args && args->type == ARGT_STR) {
1409 name = args->data.str.area;
1410 len = args->data.str.data;
1411 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001412
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001413 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001414
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001415 idx = &smp->strm->txn->hdr_idx;
1416 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 +02001417
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001418 ctx.idx = 0;
1419 cnt = 0;
1420 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
1421 cnt++;
1422 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001423
1424 smp->data.type = SMP_T_SINT;
1425 smp->data.u.sint = cnt;
1426 smp->flags = SMP_F_VOL_HDR;
1427 return 1;
1428}
1429
1430/* Fetch an HTTP header's integer value. The integer value is returned. It
1431 * takes a mandatory argument of type string and an optional one of type int
1432 * to designate a specific occurrence. It returns an unsigned integer, which
1433 * may or may not be appropriate for everything.
1434 */
1435static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1436{
1437 int ret = smp_fetch_hdr(args, smp, kw, private);
1438
1439 if (ret > 0) {
1440 smp->data.type = SMP_T_SINT;
1441 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1442 smp->data.u.str.data);
1443 }
1444
1445 return ret;
1446}
1447
1448/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1449 * and an optional one of type int to designate a specific occurrence.
1450 * It returns an IPv4 or IPv6 address.
1451 */
1452static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1453{
1454 int ret;
1455
1456 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
1457 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
1458 smp->data.type = SMP_T_IPV4;
1459 break;
1460 } else {
1461 struct buffer *temp = get_trash_chunk();
1462 if (smp->data.u.str.data < temp->size - 1) {
1463 memcpy(temp->area, smp->data.u.str.area,
1464 smp->data.u.str.data);
1465 temp->area[smp->data.u.str.data] = '\0';
1466 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1467 smp->data.type = SMP_T_IPV6;
1468 break;
1469 }
1470 }
1471 }
1472
1473 /* if the header doesn't match an IP address, fetch next one */
1474 if (!(smp->flags & SMP_F_NOT_LAST))
1475 return 0;
1476 }
1477 return ret;
1478}
1479
1480/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1481 * the first '/' after the possible hostname, and ends before the possible '?'.
1482 */
1483static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1484{
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001485 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1486 /* HTX version */
1487 struct htx *htx = smp_prefetch_htx(smp, args);
1488 union h1_sl sl;
1489 struct ist path;
1490 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001491
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001492 if (!htx)
1493 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001494
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001495 sl = http_find_stline(htx);
1496 path = http_get_path(sl.rq.u);
1497 if (!path.ptr)
1498 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001499
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001500 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Willy Tarreau79e57332018-10-02 16:01:16 +02001501
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001502 /* OK, we got the '/' ! */
1503 smp->data.type = SMP_T_STR;
1504 smp->data.u.str.area = path.ptr;
1505 smp->data.u.str.data = len;
1506 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1507 }
1508 else {
1509 struct http_txn *txn;
1510 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001511
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001512 CHECK_HTTP_MESSAGE_FIRST();
1513
1514 txn = smp->strm->txn;
1515 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1516 ptr = http_txn_get_path(txn);
1517 if (!ptr)
1518 return 0;
1519
1520 /* OK, we got the '/' ! */
1521 smp->data.type = SMP_T_STR;
1522 smp->data.u.str.area = ptr;
1523
1524 while (ptr < end && *ptr != '?')
1525 ptr++;
1526
1527 smp->data.u.str.data = ptr - smp->data.u.str.area;
1528 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1529 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001530 return 1;
1531}
1532
1533/* This produces a concatenation of the first occurrence of the Host header
1534 * followed by the path component if it begins with a slash ('/'). This means
1535 * that '*' will not be added, resulting in exactly the first Host entry.
1536 * If no Host header is found, then the path is returned as-is. The returned
1537 * value is stored in the trash so it does not need to be marked constant.
1538 * The returned sample is of type string.
1539 */
1540static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1541{
Willy Tarreau79e57332018-10-02 16:01:16 +02001542 struct buffer *temp;
1543
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001544 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1545 /* HTX version */
1546 struct htx *htx = smp_prefetch_htx(smp, args);
1547 union h1_sl sl;
1548 struct http_hdr_ctx ctx;
1549 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001550
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001551 if (!htx)
1552 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001553
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001554 ctx.blk = NULL;
1555 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1556 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001557
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001558 /* OK we have the header value in ctx.value */
1559 temp = get_trash_chunk();
1560 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1561
1562 /* now retrieve the path */
1563 sl = http_find_stline(htx);
1564 path = http_get_path(sl.rq.u);
1565 if (path.ptr) {
1566 size_t len;
1567
1568 for (len = 0; len < path.len && *(path.ptr) != '?'; len++);
1569 if (len && *(path.ptr) == '/')
1570 chunk_memcat(temp, path.ptr, len);
1571 }
1572
1573 smp->data.type = SMP_T_STR;
1574 smp->data.u.str = *temp;
1575 }
1576 else {
1577 /* LEGACY version */
1578 struct http_txn *txn;
1579 char *ptr, *end, *beg;
1580 struct hdr_ctx ctx;
1581
1582 CHECK_HTTP_MESSAGE_FIRST();
1583
1584 txn = smp->strm->txn;
1585 ctx.idx = 0;
1586 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
1587 return smp_fetch_path(args, smp, kw, private);
1588
1589 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1590 temp = get_trash_chunk();
1591 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1592 smp->data.type = SMP_T_STR;
1593 smp->data.u.str.area = temp->area;
1594 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001595
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001596 /* now retrieve the path */
1597 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1598 beg = http_txn_get_path(txn);
1599 if (!beg)
1600 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001601
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001602 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1603
1604 if (beg < ptr && *beg == '/') {
1605 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1606 ptr - beg);
1607 smp->data.u.str.data += ptr - beg;
1608 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001609 }
1610
1611 smp->flags = SMP_F_VOL_1ST;
1612 return 1;
1613}
1614
1615/* This produces a 32-bit hash of the concatenation of the first occurrence of
1616 * the Host header followed by the path component if it begins with a slash ('/').
1617 * This means that '*' will not be added, resulting in exactly the first Host
1618 * entry. If no Host header is found, then the path is used. The resulting value
1619 * is hashed using the path hash followed by a full avalanche hash and provides a
1620 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1621 * high-traffic sites without having to store whole paths.
1622 */
1623static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1624{
Willy Tarreau79e57332018-10-02 16:01:16 +02001625 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001626
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001627 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1628 /* HTX version */
1629 struct htx *htx = smp_prefetch_htx(smp, args);
1630 union h1_sl sl;
1631 struct http_hdr_ctx ctx;
1632 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001633
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001634 if (!htx)
1635 return 0;
1636
1637 ctx.blk = NULL;
1638 if (!http_find_header(htx, ist("Host"), &ctx, 0)) {
1639 /* OK we have the header value in ctx.value */
1640 while (ctx.value.len--)
1641 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1642 }
1643
1644 /* now retrieve the path */
1645 sl = http_find_stline(htx);
1646 path = http_get_path(sl.rq.u);
1647 if (path.ptr) {
1648 size_t len;
1649
1650 for (len = 0; len < path.len && *(path.ptr) != '?'; len++);
1651 if (len && *(path.ptr) == '/') {
1652 while (len--)
1653 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1654 }
1655 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001656 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001657 else {
1658 /* LEGACY version */
1659 struct http_txn *txn;
1660 struct hdr_ctx ctx;
1661 char *ptr, *beg, *end;
1662 int len;
1663
1664 CHECK_HTTP_MESSAGE_FIRST();
1665
1666 txn = smp->strm->txn;
1667 ctx.idx = 0;
1668 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
1669 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1670 ptr = ctx.line + ctx.val;
1671 len = ctx.vlen;
1672 while (len--)
1673 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1674 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001675
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001676 /* now retrieve the path */
1677 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1678 beg = http_txn_get_path(txn);
1679 if (!beg)
1680 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001681
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001682 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001683
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001684 if (beg < ptr && *beg == '/') {
1685 while (beg < ptr)
1686 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
1687 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001688 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001689
Willy Tarreau79e57332018-10-02 16:01:16 +02001690 hash = full_hash(hash);
1691
1692 smp->data.type = SMP_T_SINT;
1693 smp->data.u.sint = hash;
1694 smp->flags = SMP_F_VOL_1ST;
1695 return 1;
1696}
1697
1698/* This concatenates the source address with the 32-bit hash of the Host and
1699 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1700 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1701 * on the source address length. The path hash is stored before the address so
1702 * that in environments where IPv6 is insignificant, truncating the output to
1703 * 8 bytes would still work.
1704 */
1705static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1706{
1707 struct buffer *temp;
1708 struct connection *cli_conn = objt_conn(smp->sess->origin);
1709
1710 if (!cli_conn)
1711 return 0;
1712
1713 if (!smp_fetch_base32(args, smp, kw, private))
1714 return 0;
1715
1716 temp = get_trash_chunk();
1717 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1718 temp->data += sizeof(unsigned int);
1719
1720 switch (cli_conn->addr.from.ss_family) {
1721 case AF_INET:
1722 memcpy(temp->area + temp->data,
1723 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1724 4);
1725 temp->data += 4;
1726 break;
1727 case AF_INET6:
1728 memcpy(temp->area + temp->data,
1729 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1730 16);
1731 temp->data += 16;
1732 break;
1733 default:
1734 return 0;
1735 }
1736
1737 smp->data.u.str = *temp;
1738 smp->data.type = SMP_T_BIN;
1739 return 1;
1740}
1741
1742/* Extracts the query string, which comes after the question mark '?'. If no
1743 * question mark is found, nothing is returned. Otherwise it returns a sample
1744 * of type string carrying the whole query string.
1745 */
1746static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1747{
Willy Tarreau79e57332018-10-02 16:01:16 +02001748 char *ptr, *end;
1749
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001750 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1751 /* HTX version */
1752 struct htx *htx = smp_prefetch_htx(smp, args);
1753 union h1_sl sl;
1754
1755 if (!htx)
1756 return 0;
1757
1758 sl = http_find_stline(htx);
1759 ptr = sl.rq.u.ptr;
1760 end = sl.rq.u.ptr + sl.rq.u.len;
1761 }
1762 else {
1763 /* LEGACY version */
1764 struct http_txn *txn;
1765
1766 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001767
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001768 txn = smp->strm->txn;
1769 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.u;
1770 end = ptr + txn->req.sl.rq.u_l;
1771 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001772
1773 /* look up the '?' */
1774 do {
1775 if (ptr == end)
1776 return 0;
1777 } while (*ptr++ != '?');
1778
1779 smp->data.type = SMP_T_STR;
1780 smp->data.u.str.area = ptr;
1781 smp->data.u.str.data = end - ptr;
1782 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1783 return 1;
1784}
1785
1786static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1787{
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001788 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1789 /* HTX version */
1790 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +02001791
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001792 if (!htx)
1793 return 0;
1794 }
1795 else {
1796 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02001797
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001798 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
1799 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
1800 */
1801 CHECK_HTTP_MESSAGE_FIRST_PERM();
1802 }
1803 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001804 smp->data.u.sint = 1;
1805 return 1;
1806}
1807
1808/* return a valid test if the current request is the first one on the connection */
1809static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1810{
1811 smp->data.type = SMP_T_BOOL;
1812 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1813 return 1;
1814}
1815
1816/* Accepts exactly 1 argument of type userlist */
1817static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1818{
1819
1820 if (!args || args->type != ARGT_USR)
1821 return 0;
1822
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001823 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1824 /* HTX version */
1825 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +02001826
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001827 if (!htx)
1828 return 0;
1829 }
1830 else {
1831 /* LEGACY version */
1832 CHECK_HTTP_MESSAGE_FIRST();
1833 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001834
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001835 if (!get_http_auth(smp))
1836 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001837 smp->data.type = SMP_T_BOOL;
1838 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001839 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001840 return 1;
1841}
1842
1843/* Accepts exactly 1 argument of type userlist */
1844static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1845{
1846 if (!args || args->type != ARGT_USR)
1847 return 0;
1848
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001849 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1850 /* HTX version */
1851 struct htx *htx = smp_prefetch_htx(smp, args);
1852
1853 if (!htx)
1854 return 0;
1855 }
1856 else {
1857 /* LEGACY version */
1858 CHECK_HTTP_MESSAGE_FIRST();
1859 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001860
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001861 if (!get_http_auth(smp))
Willy Tarreau79e57332018-10-02 16:01:16 +02001862 return 0;
1863
1864 /* if the user does not belong to the userlist or has a wrong password,
1865 * report that it unconditionally does not match. Otherwise we return
1866 * a string containing the username.
1867 */
1868 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1869 smp->strm->txn->auth.pass))
1870 return 0;
1871
1872 /* pat_match_auth() will need the user list */
1873 smp->ctx.a[0] = args->data.usr;
1874
1875 smp->data.type = SMP_T_STR;
1876 smp->flags = SMP_F_CONST;
1877 smp->data.u.str.area = smp->strm->txn->auth.user;
1878 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1879
1880 return 1;
1881}
1882
1883/* Fetch a captured HTTP request header. The index is the position of
1884 * the "capture" option in the configuration file
1885 */
1886static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1887{
1888 struct proxy *fe = strm_fe(smp->strm);
1889 int idx;
1890
1891 if (!args || args->type != ARGT_SINT)
1892 return 0;
1893
1894 idx = args->data.sint;
1895
1896 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1897 return 0;
1898
1899 smp->data.type = SMP_T_STR;
1900 smp->flags |= SMP_F_CONST;
1901 smp->data.u.str.area = smp->strm->req_cap[idx];
1902 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1903
1904 return 1;
1905}
1906
1907/* Fetch a captured HTTP response header. The index is the position of
1908 * the "capture" option in the configuration file
1909 */
1910static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1911{
1912 struct proxy *fe = strm_fe(smp->strm);
1913 int idx;
1914
1915 if (!args || args->type != ARGT_SINT)
1916 return 0;
1917
1918 idx = args->data.sint;
1919
1920 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1921 return 0;
1922
1923 smp->data.type = SMP_T_STR;
1924 smp->flags |= SMP_F_CONST;
1925 smp->data.u.str.area = smp->strm->res_cap[idx];
1926 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1927
1928 return 1;
1929}
1930
1931/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1932static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1933{
1934 struct buffer *temp;
1935 struct http_txn *txn = smp->strm->txn;
1936 char *ptr;
1937
1938 if (!txn || !txn->uri)
1939 return 0;
1940
1941 ptr = txn->uri;
1942
1943 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1944 ptr++;
1945
1946 temp = get_trash_chunk();
1947 temp->area = txn->uri;
1948 temp->data = ptr - txn->uri;
1949 smp->data.u.str = *temp;
1950 smp->data.type = SMP_T_STR;
1951 smp->flags = SMP_F_CONST;
1952
1953 return 1;
1954
1955}
1956
1957/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1958static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1959{
1960 struct http_txn *txn = smp->strm->txn;
1961 struct ist path;
1962 const char *ptr;
1963
1964 if (!txn || !txn->uri)
1965 return 0;
1966
1967 ptr = txn->uri;
1968
1969 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1970 ptr++;
1971
1972 if (!*ptr)
1973 return 0;
1974
Christopher Faulet78337bb2018-11-15 14:35:18 +01001975 /* skip the first space and find space after URI */
1976 path = ist2(++ptr, 0);
1977 while (*ptr != ' ' && *ptr != '\0')
1978 ptr++;
1979 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001980
Christopher Faulet78337bb2018-11-15 14:35:18 +01001981 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02001982 if (!path.ptr)
1983 return 0;
1984
1985 smp->data.u.str.area = path.ptr;
1986 smp->data.u.str.data = path.len;
1987 smp->data.type = SMP_T_STR;
1988 smp->flags = SMP_F_CONST;
1989
1990 return 1;
1991}
1992
1993/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1994 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1995 */
1996static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1997{
1998 struct http_txn *txn = smp->strm->txn;
1999
2000 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2001 return 0;
2002
2003 if (txn->req.flags & HTTP_MSGF_VER_11)
2004 smp->data.u.str.area = "HTTP/1.1";
2005 else
2006 smp->data.u.str.area = "HTTP/1.0";
2007
2008 smp->data.u.str.data = 8;
2009 smp->data.type = SMP_T_STR;
2010 smp->flags = SMP_F_CONST;
2011 return 1;
2012
2013}
2014
2015/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2016 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2017 */
2018static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2019{
2020 struct http_txn *txn = smp->strm->txn;
2021
2022 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2023 return 0;
2024
2025 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2026 smp->data.u.str.area = "HTTP/1.1";
2027 else
2028 smp->data.u.str.area = "HTTP/1.0";
2029
2030 smp->data.u.str.data = 8;
2031 smp->data.type = SMP_T_STR;
2032 smp->flags = SMP_F_CONST;
2033 return 1;
2034
2035}
2036
2037/* Iterate over all cookies present in a message. The context is stored in
2038 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2039 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2040 * the direction, multiple cookies may be parsed on the same line or not.
2041 * The cookie name is in args and the name length in args->data.str.len.
2042 * Accepts exactly 1 argument of type string. If the input options indicate
2043 * that no iterating is desired, then only last value is fetched if any.
2044 * The returned sample is of type CSTR. Can be used to parse cookies in other
2045 * files.
2046 */
2047static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2048{
Willy Tarreau79e57332018-10-02 16:01:16 +02002049 int occ = 0;
2050 int found = 0;
2051
2052 if (!args || args->type != ARGT_STR)
2053 return 0;
2054
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002055 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2056 /* HTX version */
2057 struct htx *htx = smp_prefetch_htx(smp, args);
2058 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2059 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002060
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002061 if (!ctx) {
2062 /* first call */
2063 ctx = &static_http_hdr_ctx;
2064 ctx->blk = NULL;
2065 smp->ctx.a[2] = ctx;
2066 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002067
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002068 if (!htx)
2069 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002070
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002071 hdr = (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2072 ? ist("Cookie")
2073 : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002074
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002075 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2076 /* no explicit occurrence and single fetch => last cookie by default */
2077 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02002078
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002079 /* OK so basically here, either we want only one value and it's the
2080 * last one, or we want to iterate over all of them and we fetch the
2081 * next one.
Willy Tarreau79e57332018-10-02 16:01:16 +02002082 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002083
2084 if (!(smp->flags & SMP_F_NOT_LAST)) {
2085 /* search for the header from the beginning, we must first initialize
2086 * the search parameters.
2087 */
2088 smp->ctx.a[0] = NULL;
2089 ctx->blk = NULL;
2090 }
2091
2092 smp->flags |= SMP_F_VOL_HDR;
2093 while (1) {
2094 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2095 if (!smp->ctx.a[0]) {
2096 if (!http_find_header(htx, hdr, ctx, 0))
2097 goto out;
2098
2099 if (ctx->value.len < args->data.str.data + 1)
2100 continue;
2101
2102 smp->ctx.a[0] = ctx->value.ptr;
2103 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2104 }
2105
2106 smp->data.type = SMP_T_STR;
2107 smp->flags |= SMP_F_CONST;
2108 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2109 args->data.str.area, args->data.str.data,
2110 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2111 &smp->data.u.str.area,
2112 &smp->data.u.str.data);
2113 if (smp->ctx.a[0]) {
2114 found = 1;
2115 if (occ >= 0) {
2116 /* one value was returned into smp->data.u.str.{str,len} */
2117 smp->flags |= SMP_F_NOT_LAST;
2118 return 1;
2119 }
2120 }
2121 /* if we're looking for last occurrence, let's loop */
2122 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002123 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002124 else {
2125 /* LEGACY version */
2126 struct http_txn *txn;
2127 struct hdr_idx *idx;
2128 struct hdr_ctx *ctx = smp->ctx.a[2];
2129 const struct http_msg *msg;
2130 const char *hdr_name;
2131 int hdr_name_len;
2132 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002133
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002134 if (!ctx) {
2135 /* first call */
2136 ctx = &static_hdr_ctx;
2137 ctx->idx = 0;
2138 smp->ctx.a[2] = ctx;
2139 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002140
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002141 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02002142
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002143 txn = smp->strm->txn;
2144 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +02002145
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002146 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
2147 msg = &txn->req;
2148 hdr_name = "Cookie";
2149 hdr_name_len = 6;
2150 } else {
2151 msg = &txn->rsp;
2152 hdr_name = "Set-Cookie";
2153 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002154 }
2155
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002156 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2157 /* no explicit occurrence and single fetch => last cookie by default */
2158 occ = -1;
2159
2160 /* OK so basically here, either we want only one value and it's the
2161 * last one, or we want to iterate over all of them and we fetch the
2162 * next one.
2163 */
2164
2165 sol = ci_head(msg->chn);
2166 if (!(smp->flags & SMP_F_NOT_LAST)) {
2167 /* search for the header from the beginning, we must first initialize
2168 * the search parameters.
2169 */
2170 smp->ctx.a[0] = NULL;
2171 ctx->idx = 0;
2172 }
2173
2174 smp->flags |= SMP_F_VOL_HDR;
2175
2176 while (1) {
2177 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2178 if (!smp->ctx.a[0]) {
2179 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2180 goto out;
2181
2182 if (ctx->vlen < args->data.str.data + 1)
2183 continue;
2184
2185 smp->ctx.a[0] = ctx->line + ctx->val;
2186 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2187 }
2188
2189 smp->data.type = SMP_T_STR;
2190 smp->flags |= SMP_F_CONST;
2191 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2192 args->data.str.area, args->data.str.data,
2193 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2194 &smp->data.u.str.area, &smp->data.u.str.data);
2195 if (smp->ctx.a[0]) {
2196 found = 1;
2197 if (occ >= 0) {
2198 /* one value was returned into smp->data.u.str.{str,len} */
2199 smp->flags |= SMP_F_NOT_LAST;
2200 return 1;
2201 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002202 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002203 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002204 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002205 }
2206 /* all cookie headers and values were scanned. If we're looking for the
2207 * last occurrence, we may return it now.
2208 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002209 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002210 smp->flags &= ~SMP_F_NOT_LAST;
2211 return found;
2212}
2213
2214/* Iterate over all cookies present in a request to count how many occurrences
2215 * match the name in args and args->data.str.len. If <multi> is non-null, then
2216 * multiple cookies may be parsed on the same line. The returned sample is of
2217 * type UINT. Accepts exactly 1 argument of type string.
2218 */
2219static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2220{
Willy Tarreau79e57332018-10-02 16:01:16 +02002221 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002222 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002223
2224 if (!args || args->type != ARGT_STR)
2225 return 0;
2226
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002227 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2228 /* HTX version */
2229 struct htx *htx = smp_prefetch_htx(smp, args);
2230 struct http_hdr_ctx ctx;
2231 struct ist hdr;
2232
2233 if (!htx)
2234 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002235
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002236 hdr = (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2237 ? ist("Cookie")
2238 : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002239
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002240 val_end = val_beg = NULL;
2241 ctx.blk = NULL;
2242 cnt = 0;
2243 while (1) {
2244 /* Note: val_beg == NULL every time we need to fetch a new header */
2245 if (!val_beg) {
2246 if (!http_find_header(htx, hdr, &ctx, 0))
2247 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002248
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002249 if (ctx.value.len < args->data.str.data + 1)
2250 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002251
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002252 val_beg = ctx.value.ptr;
2253 val_end = val_beg + ctx.value.len;
2254 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002255
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002256 smp->data.type = SMP_T_STR;
2257 smp->flags |= SMP_F_CONST;
2258 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2259 args->data.str.area, args->data.str.data,
2260 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2261 &smp->data.u.str.area,
2262 &smp->data.u.str.data))) {
2263 cnt++;
2264 }
2265 }
2266 }
2267 else {
2268 /* LEGACY version */
2269 struct http_txn *txn;
2270 struct hdr_idx *idx;
2271 struct hdr_ctx ctx;
2272 const struct http_msg *msg;
2273 const char *hdr_name;
2274 int hdr_name_len;
2275 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002276
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002277 CHECK_HTTP_MESSAGE_FIRST();
2278
2279 txn = smp->strm->txn;
2280 idx = &smp->strm->txn->hdr_idx;
2281
2282 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
2283 msg = &txn->req;
2284 hdr_name = "Cookie";
2285 hdr_name_len = 6;
2286 } else {
2287 msg = &txn->rsp;
2288 hdr_name = "Set-Cookie";
2289 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002290 }
2291
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002292 sol = ci_head(msg->chn);
2293 val_end = val_beg = NULL;
2294 ctx.idx = 0;
2295 cnt = 0;
2296
2297 while (1) {
2298 /* Note: val_beg == NULL every time we need to fetch a new header */
2299 if (!val_beg) {
2300 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2301 break;
2302
2303 if (ctx.vlen < args->data.str.data + 1)
2304 continue;
2305
2306 val_beg = ctx.line + ctx.val;
2307 val_end = val_beg + ctx.vlen;
2308 }
2309
2310 smp->data.type = SMP_T_STR;
2311 smp->flags |= SMP_F_CONST;
2312 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2313 args->data.str.area, args->data.str.data,
2314 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2315 &smp->data.u.str.area, &smp->data.u.str.data))) {
2316 cnt++;
2317 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002318 }
2319 }
2320
2321 smp->data.type = SMP_T_SINT;
2322 smp->data.u.sint = cnt;
2323 smp->flags |= SMP_F_VOL_HDR;
2324 return 1;
2325}
2326
2327/* Fetch an cookie's integer value. The integer value is returned. It
2328 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2329 */
2330static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2331{
2332 int ret = smp_fetch_cookie(args, smp, kw, private);
2333
2334 if (ret > 0) {
2335 smp->data.type = SMP_T_SINT;
2336 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2337 smp->data.u.str.data);
2338 }
2339
2340 return ret;
2341}
2342
2343/************************************************************************/
2344/* The code below is dedicated to sample fetches */
2345/************************************************************************/
2346
2347/* This scans a URL-encoded query string. It takes an optionally wrapping
2348 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2349 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2350 * pointers are updated for next iteration before leaving.
2351 */
2352static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2353{
2354 const char *vstart, *vend;
2355 struct buffer *temp;
2356 const char **chunks = (const char **)smp->ctx.a;
2357
2358 if (!http_find_next_url_param(chunks, name, name_len,
2359 &vstart, &vend, delim))
2360 return 0;
2361
2362 /* Create sample. If the value is contiguous, return the pointer as CONST,
2363 * if the value is wrapped, copy-it in a buffer.
2364 */
2365 smp->data.type = SMP_T_STR;
2366 if (chunks[2] &&
2367 vstart >= chunks[0] && vstart <= chunks[1] &&
2368 vend >= chunks[2] && vend <= chunks[3]) {
2369 /* Wrapped case. */
2370 temp = get_trash_chunk();
2371 memcpy(temp->area, vstart, chunks[1] - vstart);
2372 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2373 vend - chunks[2]);
2374 smp->data.u.str.area = temp->area;
2375 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2376 } else {
2377 /* Contiguous case. */
2378 smp->data.u.str.area = (char *)vstart;
2379 smp->data.u.str.data = vend - vstart;
2380 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2381 }
2382
2383 /* Update context, check wrapping. */
2384 chunks[0] = vend;
2385 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2386 chunks[1] = chunks[3];
2387 chunks[2] = NULL;
2388 }
2389
2390 if (chunks[0] < chunks[1])
2391 smp->flags |= SMP_F_NOT_LAST;
2392
2393 return 1;
2394}
2395
2396/* This function iterates over each parameter of the query string. It uses
2397 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2398 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2399 * An optional parameter name is passed in args[0], otherwise any parameter is
2400 * considered. It supports an optional delimiter argument for the beginning of
2401 * the string in args[1], which defaults to "?".
2402 */
2403static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2404{
Willy Tarreau79e57332018-10-02 16:01:16 +02002405 char delim = '?';
2406 const char *name;
2407 int name_len;
2408
2409 if (!args ||
2410 (args[0].type && args[0].type != ARGT_STR) ||
2411 (args[1].type && args[1].type != ARGT_STR))
2412 return 0;
2413
2414 name = "";
2415 name_len = 0;
2416 if (args->type == ARGT_STR) {
2417 name = args->data.str.area;
2418 name_len = args->data.str.data;
2419 }
2420
2421 if (args[1].type)
2422 delim = *args[1].data.str.area;
2423
2424 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002425 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2426 /* HTX version */
2427 struct htx *htx = smp_prefetch_htx(smp, args);
2428 union h1_sl sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002429
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002430 if (!htx)
2431 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002432
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002433 sl = http_find_stline(htx);
2434 smp->ctx.a[0] = http_find_param_list(sl.rq.u.ptr, sl.rq.u.len, delim);
2435 if (!smp->ctx.a[0])
2436 return 0;
2437
2438 smp->ctx.a[1] = sl.rq.u.ptr + sl.rq.u.len;
2439 }
2440 else {
2441 /* LEGACY version */
2442 struct http_msg *msg;
2443
2444 CHECK_HTTP_MESSAGE_FIRST();
2445
2446 msg = &smp->strm->txn->req;
2447
2448 smp->ctx.a[0] = http_find_param_list(ci_head(msg->chn) + msg->sl.rq.u,
2449 msg->sl.rq.u_l, delim);
2450 if (!smp->ctx.a[0])
2451 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002452
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002453 smp->ctx.a[1] = ci_head(msg->chn) + msg->sl.rq.u + msg->sl.rq.u_l;
2454 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002455
2456 /* Assume that the context is filled with NULL pointer
2457 * before the first call.
2458 * smp->ctx.a[2] = NULL;
2459 * smp->ctx.a[3] = NULL;
2460 */
2461 }
2462
2463 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2464}
2465
2466/* This function iterates over each parameter of the body. This requires
2467 * that the body has been waited for using http-buffer-request. It uses
2468 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2469 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2470 * optional second part if the body wraps at the end of the buffer. An optional
2471 * parameter name is passed in args[0], otherwise any parameter is considered.
2472 */
2473static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2474{
Willy Tarreau79e57332018-10-02 16:01:16 +02002475 const char *name;
2476 int name_len;
2477
2478 if (!args || (args[0].type && args[0].type != ARGT_STR))
2479 return 0;
2480
2481 name = "";
2482 name_len = 0;
2483 if (args[0].type == ARGT_STR) {
2484 name = args[0].data.str.area;
2485 name_len = args[0].data.str.data;
2486 }
2487
2488 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002489 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2490 /* HTX version */
2491 struct htx *htx = smp_prefetch_htx(smp, args);
2492 struct buffer *temp;
2493 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002494
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002495 if (!htx)
2496 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002497
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002498 temp = get_trash_chunk();
2499 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
2500 struct htx_blk *blk = htx_get_blk(htx, pos);
2501 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002502
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002503 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
2504 break;
2505 if (type == HTX_BLK_DATA) {
2506 if (!htx_data_to_str(htx_get_blk_value(htx, blk), temp, 0))
2507 return 0;
2508 }
2509 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002510
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002511 smp->ctx.a[0] = temp->area;
2512 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002513
2514 /* Assume that the context is filled with NULL pointer
2515 * before the first call.
2516 * smp->ctx.a[2] = NULL;
2517 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002518 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002519 }
2520 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002521 /* LEGACY version */
2522 struct http_msg *msg;
2523 unsigned long len;
2524 unsigned long block1;
2525 char *body;
2526
2527 CHECK_HTTP_MESSAGE_FIRST();
2528
2529 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2530 msg = &smp->strm->txn->req;
2531 else
2532 msg = &smp->strm->txn->rsp;
2533
2534 len = http_body_bytes(msg);
2535 body = c_ptr(msg->chn, -http_data_rewind(msg));
2536
2537 block1 = len;
2538 if (block1 > b_wrap(&msg->chn->buf) - body)
2539 block1 = b_wrap(&msg->chn->buf) - body;
2540
2541 if (block1 == len) {
2542 /* buffer is not wrapped (or empty) */
2543 smp->ctx.a[0] = body;
2544 smp->ctx.a[1] = body + len;
2545
2546 /* Assume that the context is filled with NULL pointer
2547 * before the first call.
2548 * smp->ctx.a[2] = NULL;
2549 * smp->ctx.a[3] = NULL;
2550 */
2551 }
2552 else {
2553 /* buffer is wrapped, we need to defragment it */
2554 smp->ctx.a[0] = body;
2555 smp->ctx.a[1] = body + block1;
2556 smp->ctx.a[2] = b_orig(&msg->chn->buf);
2557 smp->ctx.a[3] = b_orig(&msg->chn->buf) + ( len - block1 );
2558 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002559 }
2560 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002561
Willy Tarreau79e57332018-10-02 16:01:16 +02002562 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2563}
2564
2565/* Return the signed integer value for the specified url parameter (see url_param
2566 * above).
2567 */
2568static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2569{
2570 int ret = smp_fetch_url_param(args, smp, kw, private);
2571
2572 if (ret > 0) {
2573 smp->data.type = SMP_T_SINT;
2574 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2575 smp->data.u.str.data);
2576 }
2577
2578 return ret;
2579}
2580
2581/* This produces a 32-bit hash of the concatenation of the first occurrence of
2582 * the Host header followed by the path component if it begins with a slash ('/').
2583 * This means that '*' will not be added, resulting in exactly the first Host
2584 * entry. If no Host header is found, then the path is used. The resulting value
2585 * is hashed using the url hash followed by a full avalanche hash and provides a
2586 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2587 * high-traffic sites without having to store whole paths.
2588 * this differs from the base32 functions in that it includes the url parameters
2589 * as well as the path
2590 */
2591static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2592{
Willy Tarreau79e57332018-10-02 16:01:16 +02002593 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002594
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002595 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2596 /* HTX version */
2597 struct htx *htx = smp_prefetch_htx(smp, args);
2598 struct http_hdr_ctx ctx;
2599 union h1_sl sl;
2600 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002601
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002602 if (!htx)
2603 return 0;
2604
2605 ctx.blk = NULL;
2606 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2607 /* OK we have the header value in ctx.value */
2608 while (ctx.value.len--)
2609 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2610 }
2611
2612 /* now retrieve the path */
2613 sl = http_find_stline(htx);
2614 path = http_get_path(sl.rq.u);
2615 while (path.len > 0 && *(path.ptr) != '?') {
2616 path.ptr++;
2617 path.len--;
2618 }
2619 if (path.len && *(path.ptr) == '/') {
2620 while (path.len--)
2621 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2622 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002623 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002624 else {
2625 /* LEGACY version */
2626 struct http_txn *txn;
2627 struct hdr_ctx ctx;
2628 char *ptr, *beg, *end;
2629 int len;
2630
2631 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02002632
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002633 txn = smp->strm->txn;
2634 ctx.idx = 0;
2635 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
2636 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2637 ptr = ctx.line + ctx.val;
2638 len = ctx.vlen;
2639 while (len--)
2640 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2641 }
2642
2643 /* now retrieve the path */
2644 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
2645 beg = http_txn_get_path(txn);
2646 if (!beg)
2647 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002648
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002649 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002650
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002651 if (beg < ptr && *beg == '/') {
2652 while (beg < ptr)
2653 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2654 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002655 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002656
Willy Tarreau79e57332018-10-02 16:01:16 +02002657 hash = full_hash(hash);
2658
2659 smp->data.type = SMP_T_SINT;
2660 smp->data.u.sint = hash;
2661 smp->flags = SMP_F_VOL_1ST;
2662 return 1;
2663}
2664
2665/* This concatenates the source address with the 32-bit hash of the Host and
2666 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2667 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2668 * on the source address length. The URL hash is stored before the address so
2669 * that in environments where IPv6 is insignificant, truncating the output to
2670 * 8 bytes would still work.
2671 */
2672static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2673{
2674 struct buffer *temp;
2675 struct connection *cli_conn = objt_conn(smp->sess->origin);
2676
2677 if (!cli_conn)
2678 return 0;
2679
2680 if (!smp_fetch_url32(args, smp, kw, private))
2681 return 0;
2682
2683 temp = get_trash_chunk();
2684 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2685 temp->data += sizeof(unsigned int);
2686
2687 switch (cli_conn->addr.from.ss_family) {
2688 case AF_INET:
2689 memcpy(temp->area + temp->data,
2690 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2691 4);
2692 temp->data += 4;
2693 break;
2694 case AF_INET6:
2695 memcpy(temp->area + temp->data,
2696 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2697 16);
2698 temp->data += 16;
2699 break;
2700 default:
2701 return 0;
2702 }
2703
2704 smp->data.u.str = *temp;
2705 smp->data.type = SMP_T_BIN;
2706 return 1;
2707}
2708
2709/************************************************************************/
2710/* Other utility functions */
2711/************************************************************************/
2712
2713/* This function is used to validate the arguments passed to any "hdr" fetch
2714 * keyword. These keywords support an optional positive or negative occurrence
2715 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2716 * is assumed that the types are already the correct ones. Returns 0 on error,
2717 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2718 * error message in case of error, that the caller is responsible for freeing.
2719 * The initial location must either be freeable or NULL.
2720 * Note: this function's pointer is checked from Lua.
2721 */
2722int val_hdr(struct arg *arg, char **err_msg)
2723{
2724 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2725 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2726 return 0;
2727 }
2728 return 1;
2729}
2730
2731/************************************************************************/
2732/* All supported sample fetch keywords must be declared here. */
2733/************************************************************************/
2734
2735/* Note: must not be declared <const> as its list will be overwritten */
2736static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2737 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2738 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2739 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2740
2741 /* capture are allocated and are permanent in the stream */
2742 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2743
2744 /* retrieve these captures from the HTTP logs */
2745 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2746 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2747 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2748
2749 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2750 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2751
2752 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2753 * are only here to match the ACL's name, are request-only and are used
2754 * for ACL compatibility only.
2755 */
2756 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2757 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
2758 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2759 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2760
2761 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2762 * only here to match the ACL's name, are request-only and are used for
2763 * ACL compatibility only.
2764 */
2765 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
2766 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2767 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2768 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2769
2770 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2771 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2772 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2773 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2774 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2775 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2776
2777 /* HTTP protocol on the request path */
2778 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2779 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2780
2781 /* HTTP version on the request path */
2782 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2783 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2784
2785 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2786 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2787 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2788 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2789
2790 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2791 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2792
2793 /* HTTP version on the response path */
2794 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2795 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2796
2797 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2798 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2799 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2800 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2801
2802 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2803 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2804 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2805 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2806 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2807 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2808 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2809
2810 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2811 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2812 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2813 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2814
2815 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2816 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2817 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2818 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2819 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2820 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2821 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2822
2823 /* scook is valid only on the response and is used for ACL compatibility */
2824 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2825 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2826 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2827 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2828
2829 /* shdr is valid only on the response and is used for ACL compatibility */
2830 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2831 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2832 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2833 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2834
2835 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2836 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2837 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2838 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2839 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2840 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2841 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2842 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2843 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2844 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2845 { /* END */ },
2846}};
2847
2848
2849__attribute__((constructor))
2850static void __http_fetch_init(void)
2851{
2852 sample_register_fetches(&sample_fetch_keywords);
2853}
2854
2855/*
2856 * Local variables:
2857 * c-indent-level: 8
2858 * c-basic-offset: 8
2859 * End:
2860 */