blob: 56c6a2bf90323c961517a2cdf78aba66661f9d7b [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;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001168 } else {
1169 name.ptr = NULL;
1170 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001171 }
1172
1173 ctx.blk = NULL;
1174 cnt = 0;
1175 while (http_find_header(htx, name, &ctx, 1))
1176 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001177 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001178 else {
1179 /* LEGACY version */
1180 struct hdr_idx *idx;
1181 struct hdr_ctx ctx;
1182 const struct http_msg *msg;
1183 const char *name = NULL;
1184 int len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001185
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001186 if (args && args->type == ARGT_STR) {
1187 name = args->data.str.area;
1188 len = args->data.str.data;
1189 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001190
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001191 CHECK_HTTP_MESSAGE_FIRST();
1192
1193 idx = &smp->strm->txn->hdr_idx;
1194 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 +02001195
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001196 ctx.idx = 0;
1197 cnt = 0;
1198 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
1199 cnt++;
1200 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001201
1202 smp->data.type = SMP_T_SINT;
1203 smp->data.u.sint = cnt;
1204 smp->flags = SMP_F_VOL_HDR;
1205 return 1;
1206}
1207
1208static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
1209{
Willy Tarreau79e57332018-10-02 16:01:16 +02001210 struct buffer *temp;
1211 char del = ',';
1212
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001213 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1214 /* HTX version */
1215 struct htx *htx = smp_prefetch_htx(smp, args);
1216 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001217
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001218 if (!htx)
1219 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001220
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001221 if (args && args->type == ARGT_STR)
1222 del = *args[0].data.str.area;
Willy Tarreau79e57332018-10-02 16:01:16 +02001223
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001224 temp = get_trash_chunk();
1225 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1226 struct htx_blk *blk = htx_get_blk(htx, pos);
1227 enum htx_blk_type type = htx_get_blk_type(blk);
1228 struct ist n;
Willy Tarreau79e57332018-10-02 16:01:16 +02001229
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001230 if (type == HTX_BLK_EOH)
1231 break;
1232 if (type != HTX_BLK_HDR)
1233 continue;
1234 n = htx_get_blk_name(htx, blk);
1235
1236 if (temp->data)
1237 temp->area[temp->data++] = del;
1238 chunk_memcat(temp, n.ptr, n.len);
1239 }
1240 }
1241 else {
1242 /* LEGACY version */
1243 struct hdr_idx *idx;
1244 struct hdr_ctx ctx;
1245 const struct http_msg *msg;
1246
1247 if (args && args->type == ARGT_STR)
1248 del = *args[0].data.str.area;
1249
1250 CHECK_HTTP_MESSAGE_FIRST();
1251
1252 idx = &smp->strm->txn->hdr_idx;
1253 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
1254
1255 temp = get_trash_chunk();
1256
1257 ctx.idx = 0;
1258 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
1259 if (temp->data)
1260 temp->area[temp->data++] = del;
1261 memcpy(temp->area + temp->data, ctx.line, ctx.del);
1262 temp->data += ctx.del;
1263 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001264 }
1265
1266 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001267 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001268 smp->flags = SMP_F_VOL_HDR;
1269 return 1;
1270}
1271
1272/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1273 * Accepts an optional argument of type string containing the header field name,
1274 * and an optional argument of type signed or unsigned integer to request an
1275 * explicit occurrence of the header. Note that in the event of a missing name,
1276 * headers are considered from the first one.
1277 */
1278static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1279{
Willy Tarreau79e57332018-10-02 16:01:16 +02001280 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001281
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001282 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1283 /* HTX version */
1284 struct htx *htx = smp_prefetch_htx(smp, args);
1285 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1286 struct ist name;
1287
1288 if (!ctx) {
1289 /* first call */
1290 ctx = &static_http_hdr_ctx;
1291 ctx->blk = NULL;
1292 smp->ctx.a[0] = ctx;
1293 }
1294
1295 if (args) {
1296 if (args[0].type != ARGT_STR)
1297 return 0;
1298 name.ptr = args[0].data.str.area;
1299 name.len = args[0].data.str.data;
1300
1301 if (args[1].type == ARGT_SINT)
1302 occ = args[1].data.sint;
1303 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001304
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001305 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001306 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001307
1308 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1309 /* search for header from the beginning */
1310 ctx->blk = NULL;
1311
1312 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1313 /* no explicit occurrence and single fetch => last header by default */
1314 occ = -1;
1315
1316 if (!occ)
1317 /* prepare to report multiple occurrences for ACL fetches */
1318 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001319
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001320 smp->data.type = SMP_T_STR;
1321 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1322 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1323 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001324 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001325 else {
1326 /* LEGACY version */
1327 struct hdr_idx *idx;
1328 struct hdr_ctx *ctx = smp->ctx.a[0];
1329 const struct http_msg *msg;
1330 const char *name_str = NULL;
1331 int name_len = 0;
1332
1333 if (!ctx) {
1334 /* first call */
1335 ctx = &static_hdr_ctx;
1336 ctx->idx = 0;
1337 smp->ctx.a[0] = ctx;
1338 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001339
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001340 if (args) {
1341 if (args[0].type != ARGT_STR)
1342 return 0;
1343 name_str = args[0].data.str.area;
1344 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001345
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001346 if (args[1].type == ARGT_SINT)
1347 occ = args[1].data.sint;
1348 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001349
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001350 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001351
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001352 idx = &smp->strm->txn->hdr_idx;
1353 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 +02001354
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001355 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1356 /* search for header from the beginning */
1357 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001358
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001359 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1360 /* no explicit occurrence and single fetch => last header by default */
1361 occ = -1;
1362
1363 if (!occ)
1364 /* prepare to report multiple occurrences for ACL fetches */
1365 smp->flags |= SMP_F_NOT_LAST;
1366
1367 smp->data.type = SMP_T_STR;
1368 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1369 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1370 return 1;
1371 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001372
1373 smp->flags &= ~SMP_F_NOT_LAST;
1374 return 0;
1375}
1376
1377/* 6. Check on HTTP header count. The number of occurrences is returned.
1378 * Accepts exactly 1 argument of type string.
1379 */
1380static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1381{
Willy Tarreau79e57332018-10-02 16:01:16 +02001382 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001383
1384 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1385 /* HTX version */
1386 struct htx *htx = smp_prefetch_htx(smp, args);
1387 struct http_hdr_ctx ctx;
1388 struct ist name;
1389
1390 if (!htx)
1391 return 0;
1392
1393 if (args && args->type == ARGT_STR) {
1394 name.ptr = args->data.str.area;
1395 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001396 } else {
1397 name.ptr = NULL;
1398 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001399 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001400
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001401 ctx.blk = NULL;
1402 cnt = 0;
1403 while (http_find_header(htx, name, &ctx, 0))
1404 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001405 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001406 else {
1407 /* LEGACY version */
1408 struct hdr_idx *idx;
1409 struct hdr_ctx ctx;
1410 const struct http_msg *msg;
1411 const char *name = NULL;
1412 int len = 0;
1413
1414 if (args && args->type == ARGT_STR) {
1415 name = args->data.str.area;
1416 len = args->data.str.data;
1417 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001418
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001419 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001420
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001421 idx = &smp->strm->txn->hdr_idx;
1422 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 +02001423
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001424 ctx.idx = 0;
1425 cnt = 0;
1426 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
1427 cnt++;
1428 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001429
1430 smp->data.type = SMP_T_SINT;
1431 smp->data.u.sint = cnt;
1432 smp->flags = SMP_F_VOL_HDR;
1433 return 1;
1434}
1435
1436/* Fetch an HTTP header's integer value. The integer value is returned. It
1437 * takes a mandatory argument of type string and an optional one of type int
1438 * to designate a specific occurrence. It returns an unsigned integer, which
1439 * may or may not be appropriate for everything.
1440 */
1441static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1442{
1443 int ret = smp_fetch_hdr(args, smp, kw, private);
1444
1445 if (ret > 0) {
1446 smp->data.type = SMP_T_SINT;
1447 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1448 smp->data.u.str.data);
1449 }
1450
1451 return ret;
1452}
1453
1454/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1455 * and an optional one of type int to designate a specific occurrence.
1456 * It returns an IPv4 or IPv6 address.
1457 */
1458static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1459{
1460 int ret;
1461
1462 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
1463 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
1464 smp->data.type = SMP_T_IPV4;
1465 break;
1466 } else {
1467 struct buffer *temp = get_trash_chunk();
1468 if (smp->data.u.str.data < temp->size - 1) {
1469 memcpy(temp->area, smp->data.u.str.area,
1470 smp->data.u.str.data);
1471 temp->area[smp->data.u.str.data] = '\0';
1472 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1473 smp->data.type = SMP_T_IPV6;
1474 break;
1475 }
1476 }
1477 }
1478
1479 /* if the header doesn't match an IP address, fetch next one */
1480 if (!(smp->flags & SMP_F_NOT_LAST))
1481 return 0;
1482 }
1483 return ret;
1484}
1485
1486/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1487 * the first '/' after the possible hostname, and ends before the possible '?'.
1488 */
1489static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1490{
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001491 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1492 /* HTX version */
1493 struct htx *htx = smp_prefetch_htx(smp, args);
1494 union h1_sl sl;
1495 struct ist path;
1496 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001497
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001498 if (!htx)
1499 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001500
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001501 sl = http_find_stline(htx);
1502 path = http_get_path(sl.rq.u);
1503 if (!path.ptr)
1504 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001505
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001506 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Willy Tarreau79e57332018-10-02 16:01:16 +02001507
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001508 /* OK, we got the '/' ! */
1509 smp->data.type = SMP_T_STR;
1510 smp->data.u.str.area = path.ptr;
1511 smp->data.u.str.data = len;
1512 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1513 }
1514 else {
1515 struct http_txn *txn;
1516 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001517
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001518 CHECK_HTTP_MESSAGE_FIRST();
1519
1520 txn = smp->strm->txn;
1521 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1522 ptr = http_txn_get_path(txn);
1523 if (!ptr)
1524 return 0;
1525
1526 /* OK, we got the '/' ! */
1527 smp->data.type = SMP_T_STR;
1528 smp->data.u.str.area = ptr;
1529
1530 while (ptr < end && *ptr != '?')
1531 ptr++;
1532
1533 smp->data.u.str.data = ptr - smp->data.u.str.area;
1534 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1535 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001536 return 1;
1537}
1538
1539/* This produces a concatenation of the first occurrence of the Host header
1540 * followed by the path component if it begins with a slash ('/'). This means
1541 * that '*' will not be added, resulting in exactly the first Host entry.
1542 * If no Host header is found, then the path is returned as-is. The returned
1543 * value is stored in the trash so it does not need to be marked constant.
1544 * The returned sample is of type string.
1545 */
1546static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1547{
Willy Tarreau79e57332018-10-02 16:01:16 +02001548 struct buffer *temp;
1549
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001550 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1551 /* HTX version */
1552 struct htx *htx = smp_prefetch_htx(smp, args);
1553 union h1_sl sl;
1554 struct http_hdr_ctx ctx;
1555 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001556
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001557 if (!htx)
1558 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001559
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001560 ctx.blk = NULL;
1561 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1562 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001563
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001564 /* OK we have the header value in ctx.value */
1565 temp = get_trash_chunk();
1566 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1567
1568 /* now retrieve the path */
1569 sl = http_find_stline(htx);
1570 path = http_get_path(sl.rq.u);
1571 if (path.ptr) {
1572 size_t len;
1573
1574 for (len = 0; len < path.len && *(path.ptr) != '?'; len++);
1575 if (len && *(path.ptr) == '/')
1576 chunk_memcat(temp, path.ptr, len);
1577 }
1578
1579 smp->data.type = SMP_T_STR;
1580 smp->data.u.str = *temp;
1581 }
1582 else {
1583 /* LEGACY version */
1584 struct http_txn *txn;
1585 char *ptr, *end, *beg;
1586 struct hdr_ctx ctx;
1587
1588 CHECK_HTTP_MESSAGE_FIRST();
1589
1590 txn = smp->strm->txn;
1591 ctx.idx = 0;
1592 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
1593 return smp_fetch_path(args, smp, kw, private);
1594
1595 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1596 temp = get_trash_chunk();
1597 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1598 smp->data.type = SMP_T_STR;
1599 smp->data.u.str.area = temp->area;
1600 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001601
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001602 /* now retrieve the path */
1603 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1604 beg = http_txn_get_path(txn);
1605 if (!beg)
1606 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001607
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001608 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1609
1610 if (beg < ptr && *beg == '/') {
1611 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1612 ptr - beg);
1613 smp->data.u.str.data += ptr - beg;
1614 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001615 }
1616
1617 smp->flags = SMP_F_VOL_1ST;
1618 return 1;
1619}
1620
1621/* This produces a 32-bit hash of the concatenation of the first occurrence of
1622 * the Host header followed by the path component if it begins with a slash ('/').
1623 * This means that '*' will not be added, resulting in exactly the first Host
1624 * entry. If no Host header is found, then the path is used. The resulting value
1625 * is hashed using the path hash followed by a full avalanche hash and provides a
1626 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1627 * high-traffic sites without having to store whole paths.
1628 */
1629static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1630{
Willy Tarreau79e57332018-10-02 16:01:16 +02001631 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001632
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001633 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1634 /* HTX version */
1635 struct htx *htx = smp_prefetch_htx(smp, args);
1636 union h1_sl sl;
1637 struct http_hdr_ctx ctx;
1638 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001639
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001640 if (!htx)
1641 return 0;
1642
1643 ctx.blk = NULL;
1644 if (!http_find_header(htx, ist("Host"), &ctx, 0)) {
1645 /* OK we have the header value in ctx.value */
1646 while (ctx.value.len--)
1647 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1648 }
1649
1650 /* now retrieve the path */
1651 sl = http_find_stline(htx);
1652 path = http_get_path(sl.rq.u);
1653 if (path.ptr) {
1654 size_t len;
1655
1656 for (len = 0; len < path.len && *(path.ptr) != '?'; len++);
1657 if (len && *(path.ptr) == '/') {
1658 while (len--)
1659 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1660 }
1661 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001662 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001663 else {
1664 /* LEGACY version */
1665 struct http_txn *txn;
1666 struct hdr_ctx ctx;
1667 char *ptr, *beg, *end;
1668 int len;
1669
1670 CHECK_HTTP_MESSAGE_FIRST();
1671
1672 txn = smp->strm->txn;
1673 ctx.idx = 0;
1674 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
1675 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1676 ptr = ctx.line + ctx.val;
1677 len = ctx.vlen;
1678 while (len--)
1679 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1680 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001681
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001682 /* now retrieve the path */
1683 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1684 beg = http_txn_get_path(txn);
1685 if (!beg)
1686 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001687
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001688 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001689
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001690 if (beg < ptr && *beg == '/') {
1691 while (beg < ptr)
1692 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
1693 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001694 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001695
Willy Tarreau79e57332018-10-02 16:01:16 +02001696 hash = full_hash(hash);
1697
1698 smp->data.type = SMP_T_SINT;
1699 smp->data.u.sint = hash;
1700 smp->flags = SMP_F_VOL_1ST;
1701 return 1;
1702}
1703
1704/* This concatenates the source address with the 32-bit hash of the Host and
1705 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1706 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1707 * on the source address length. The path hash is stored before the address so
1708 * that in environments where IPv6 is insignificant, truncating the output to
1709 * 8 bytes would still work.
1710 */
1711static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1712{
1713 struct buffer *temp;
1714 struct connection *cli_conn = objt_conn(smp->sess->origin);
1715
1716 if (!cli_conn)
1717 return 0;
1718
1719 if (!smp_fetch_base32(args, smp, kw, private))
1720 return 0;
1721
1722 temp = get_trash_chunk();
1723 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1724 temp->data += sizeof(unsigned int);
1725
1726 switch (cli_conn->addr.from.ss_family) {
1727 case AF_INET:
1728 memcpy(temp->area + temp->data,
1729 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1730 4);
1731 temp->data += 4;
1732 break;
1733 case AF_INET6:
1734 memcpy(temp->area + temp->data,
1735 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1736 16);
1737 temp->data += 16;
1738 break;
1739 default:
1740 return 0;
1741 }
1742
1743 smp->data.u.str = *temp;
1744 smp->data.type = SMP_T_BIN;
1745 return 1;
1746}
1747
1748/* Extracts the query string, which comes after the question mark '?'. If no
1749 * question mark is found, nothing is returned. Otherwise it returns a sample
1750 * of type string carrying the whole query string.
1751 */
1752static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1753{
Willy Tarreau79e57332018-10-02 16:01:16 +02001754 char *ptr, *end;
1755
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001756 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1757 /* HTX version */
1758 struct htx *htx = smp_prefetch_htx(smp, args);
1759 union h1_sl sl;
1760
1761 if (!htx)
1762 return 0;
1763
1764 sl = http_find_stline(htx);
1765 ptr = sl.rq.u.ptr;
1766 end = sl.rq.u.ptr + sl.rq.u.len;
1767 }
1768 else {
1769 /* LEGACY version */
1770 struct http_txn *txn;
1771
1772 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001773
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001774 txn = smp->strm->txn;
1775 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.u;
1776 end = ptr + txn->req.sl.rq.u_l;
1777 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001778
1779 /* look up the '?' */
1780 do {
1781 if (ptr == end)
1782 return 0;
1783 } while (*ptr++ != '?');
1784
1785 smp->data.type = SMP_T_STR;
1786 smp->data.u.str.area = ptr;
1787 smp->data.u.str.data = end - ptr;
1788 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1789 return 1;
1790}
1791
1792static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1793{
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001794 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1795 /* HTX version */
1796 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +02001797
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001798 if (!htx)
1799 return 0;
1800 }
1801 else {
1802 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02001803
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001804 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
1805 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
1806 */
1807 CHECK_HTTP_MESSAGE_FIRST_PERM();
1808 }
1809 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001810 smp->data.u.sint = 1;
1811 return 1;
1812}
1813
1814/* return a valid test if the current request is the first one on the connection */
1815static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1816{
1817 smp->data.type = SMP_T_BOOL;
1818 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1819 return 1;
1820}
1821
1822/* Accepts exactly 1 argument of type userlist */
1823static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1824{
1825
1826 if (!args || args->type != ARGT_USR)
1827 return 0;
1828
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001829 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1830 /* HTX version */
1831 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +02001832
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001833 if (!htx)
1834 return 0;
1835 }
1836 else {
1837 /* LEGACY version */
1838 CHECK_HTTP_MESSAGE_FIRST();
1839 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001840
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001841 if (!get_http_auth(smp))
1842 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001843 smp->data.type = SMP_T_BOOL;
1844 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001845 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001846 return 1;
1847}
1848
1849/* Accepts exactly 1 argument of type userlist */
1850static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1851{
1852 if (!args || args->type != ARGT_USR)
1853 return 0;
1854
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001855 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1856 /* HTX version */
1857 struct htx *htx = smp_prefetch_htx(smp, args);
1858
1859 if (!htx)
1860 return 0;
1861 }
1862 else {
1863 /* LEGACY version */
1864 CHECK_HTTP_MESSAGE_FIRST();
1865 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001866
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001867 if (!get_http_auth(smp))
Willy Tarreau79e57332018-10-02 16:01:16 +02001868 return 0;
1869
1870 /* if the user does not belong to the userlist or has a wrong password,
1871 * report that it unconditionally does not match. Otherwise we return
1872 * a string containing the username.
1873 */
1874 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1875 smp->strm->txn->auth.pass))
1876 return 0;
1877
1878 /* pat_match_auth() will need the user list */
1879 smp->ctx.a[0] = args->data.usr;
1880
1881 smp->data.type = SMP_T_STR;
1882 smp->flags = SMP_F_CONST;
1883 smp->data.u.str.area = smp->strm->txn->auth.user;
1884 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1885
1886 return 1;
1887}
1888
1889/* Fetch a captured HTTP request header. The index is the position of
1890 * the "capture" option in the configuration file
1891 */
1892static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1893{
1894 struct proxy *fe = strm_fe(smp->strm);
1895 int idx;
1896
1897 if (!args || args->type != ARGT_SINT)
1898 return 0;
1899
1900 idx = args->data.sint;
1901
1902 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1903 return 0;
1904
1905 smp->data.type = SMP_T_STR;
1906 smp->flags |= SMP_F_CONST;
1907 smp->data.u.str.area = smp->strm->req_cap[idx];
1908 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1909
1910 return 1;
1911}
1912
1913/* Fetch a captured HTTP response header. The index is the position of
1914 * the "capture" option in the configuration file
1915 */
1916static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1917{
1918 struct proxy *fe = strm_fe(smp->strm);
1919 int idx;
1920
1921 if (!args || args->type != ARGT_SINT)
1922 return 0;
1923
1924 idx = args->data.sint;
1925
1926 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1927 return 0;
1928
1929 smp->data.type = SMP_T_STR;
1930 smp->flags |= SMP_F_CONST;
1931 smp->data.u.str.area = smp->strm->res_cap[idx];
1932 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1933
1934 return 1;
1935}
1936
1937/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1938static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1939{
1940 struct buffer *temp;
1941 struct http_txn *txn = smp->strm->txn;
1942 char *ptr;
1943
1944 if (!txn || !txn->uri)
1945 return 0;
1946
1947 ptr = txn->uri;
1948
1949 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1950 ptr++;
1951
1952 temp = get_trash_chunk();
1953 temp->area = txn->uri;
1954 temp->data = ptr - txn->uri;
1955 smp->data.u.str = *temp;
1956 smp->data.type = SMP_T_STR;
1957 smp->flags = SMP_F_CONST;
1958
1959 return 1;
1960
1961}
1962
1963/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1964static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1965{
1966 struct http_txn *txn = smp->strm->txn;
1967 struct ist path;
1968 const char *ptr;
1969
1970 if (!txn || !txn->uri)
1971 return 0;
1972
1973 ptr = txn->uri;
1974
1975 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1976 ptr++;
1977
1978 if (!*ptr)
1979 return 0;
1980
Christopher Faulet78337bb2018-11-15 14:35:18 +01001981 /* skip the first space and find space after URI */
1982 path = ist2(++ptr, 0);
1983 while (*ptr != ' ' && *ptr != '\0')
1984 ptr++;
1985 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001986
Christopher Faulet78337bb2018-11-15 14:35:18 +01001987 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02001988 if (!path.ptr)
1989 return 0;
1990
1991 smp->data.u.str.area = path.ptr;
1992 smp->data.u.str.data = path.len;
1993 smp->data.type = SMP_T_STR;
1994 smp->flags = SMP_F_CONST;
1995
1996 return 1;
1997}
1998
1999/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
2000 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2001 */
2002static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2003{
2004 struct http_txn *txn = smp->strm->txn;
2005
2006 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2007 return 0;
2008
2009 if (txn->req.flags & HTTP_MSGF_VER_11)
2010 smp->data.u.str.area = "HTTP/1.1";
2011 else
2012 smp->data.u.str.area = "HTTP/1.0";
2013
2014 smp->data.u.str.data = 8;
2015 smp->data.type = SMP_T_STR;
2016 smp->flags = SMP_F_CONST;
2017 return 1;
2018
2019}
2020
2021/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2022 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2023 */
2024static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2025{
2026 struct http_txn *txn = smp->strm->txn;
2027
2028 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2029 return 0;
2030
2031 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2032 smp->data.u.str.area = "HTTP/1.1";
2033 else
2034 smp->data.u.str.area = "HTTP/1.0";
2035
2036 smp->data.u.str.data = 8;
2037 smp->data.type = SMP_T_STR;
2038 smp->flags = SMP_F_CONST;
2039 return 1;
2040
2041}
2042
2043/* Iterate over all cookies present in a message. The context is stored in
2044 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2045 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2046 * the direction, multiple cookies may be parsed on the same line or not.
2047 * The cookie name is in args and the name length in args->data.str.len.
2048 * Accepts exactly 1 argument of type string. If the input options indicate
2049 * that no iterating is desired, then only last value is fetched if any.
2050 * The returned sample is of type CSTR. Can be used to parse cookies in other
2051 * files.
2052 */
2053static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2054{
Willy Tarreau79e57332018-10-02 16:01:16 +02002055 int occ = 0;
2056 int found = 0;
2057
2058 if (!args || args->type != ARGT_STR)
2059 return 0;
2060
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002061 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2062 /* HTX version */
2063 struct htx *htx = smp_prefetch_htx(smp, args);
2064 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2065 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002066
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002067 if (!ctx) {
2068 /* first call */
2069 ctx = &static_http_hdr_ctx;
2070 ctx->blk = NULL;
2071 smp->ctx.a[2] = ctx;
2072 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002073
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002074 if (!htx)
2075 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002076
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002077 hdr = (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2078 ? ist("Cookie")
2079 : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002080
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002081 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2082 /* no explicit occurrence and single fetch => last cookie by default */
2083 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02002084
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002085 /* OK so basically here, either we want only one value and it's the
2086 * last one, or we want to iterate over all of them and we fetch the
2087 * next one.
Willy Tarreau79e57332018-10-02 16:01:16 +02002088 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002089
2090 if (!(smp->flags & SMP_F_NOT_LAST)) {
2091 /* search for the header from the beginning, we must first initialize
2092 * the search parameters.
2093 */
2094 smp->ctx.a[0] = NULL;
2095 ctx->blk = NULL;
2096 }
2097
2098 smp->flags |= SMP_F_VOL_HDR;
2099 while (1) {
2100 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2101 if (!smp->ctx.a[0]) {
2102 if (!http_find_header(htx, hdr, ctx, 0))
2103 goto out;
2104
2105 if (ctx->value.len < args->data.str.data + 1)
2106 continue;
2107
2108 smp->ctx.a[0] = ctx->value.ptr;
2109 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2110 }
2111
2112 smp->data.type = SMP_T_STR;
2113 smp->flags |= SMP_F_CONST;
2114 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2115 args->data.str.area, args->data.str.data,
2116 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2117 &smp->data.u.str.area,
2118 &smp->data.u.str.data);
2119 if (smp->ctx.a[0]) {
2120 found = 1;
2121 if (occ >= 0) {
2122 /* one value was returned into smp->data.u.str.{str,len} */
2123 smp->flags |= SMP_F_NOT_LAST;
2124 return 1;
2125 }
2126 }
2127 /* if we're looking for last occurrence, let's loop */
2128 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002129 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002130 else {
2131 /* LEGACY version */
2132 struct http_txn *txn;
2133 struct hdr_idx *idx;
2134 struct hdr_ctx *ctx = smp->ctx.a[2];
2135 const struct http_msg *msg;
2136 const char *hdr_name;
2137 int hdr_name_len;
2138 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002139
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002140 if (!ctx) {
2141 /* first call */
2142 ctx = &static_hdr_ctx;
2143 ctx->idx = 0;
2144 smp->ctx.a[2] = ctx;
2145 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002146
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002147 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02002148
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002149 txn = smp->strm->txn;
2150 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +02002151
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002152 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
2153 msg = &txn->req;
2154 hdr_name = "Cookie";
2155 hdr_name_len = 6;
2156 } else {
2157 msg = &txn->rsp;
2158 hdr_name = "Set-Cookie";
2159 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002160 }
2161
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002162 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2163 /* no explicit occurrence and single fetch => last cookie by default */
2164 occ = -1;
2165
2166 /* OK so basically here, either we want only one value and it's the
2167 * last one, or we want to iterate over all of them and we fetch the
2168 * next one.
2169 */
2170
2171 sol = ci_head(msg->chn);
2172 if (!(smp->flags & SMP_F_NOT_LAST)) {
2173 /* search for the header from the beginning, we must first initialize
2174 * the search parameters.
2175 */
2176 smp->ctx.a[0] = NULL;
2177 ctx->idx = 0;
2178 }
2179
2180 smp->flags |= SMP_F_VOL_HDR;
2181
2182 while (1) {
2183 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2184 if (!smp->ctx.a[0]) {
2185 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2186 goto out;
2187
2188 if (ctx->vlen < args->data.str.data + 1)
2189 continue;
2190
2191 smp->ctx.a[0] = ctx->line + ctx->val;
2192 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2193 }
2194
2195 smp->data.type = SMP_T_STR;
2196 smp->flags |= SMP_F_CONST;
2197 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2198 args->data.str.area, args->data.str.data,
2199 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2200 &smp->data.u.str.area, &smp->data.u.str.data);
2201 if (smp->ctx.a[0]) {
2202 found = 1;
2203 if (occ >= 0) {
2204 /* one value was returned into smp->data.u.str.{str,len} */
2205 smp->flags |= SMP_F_NOT_LAST;
2206 return 1;
2207 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002208 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002209 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002210 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002211 }
2212 /* all cookie headers and values were scanned. If we're looking for the
2213 * last occurrence, we may return it now.
2214 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002215 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002216 smp->flags &= ~SMP_F_NOT_LAST;
2217 return found;
2218}
2219
2220/* Iterate over all cookies present in a request to count how many occurrences
2221 * match the name in args and args->data.str.len. If <multi> is non-null, then
2222 * multiple cookies may be parsed on the same line. The returned sample is of
2223 * type UINT. Accepts exactly 1 argument of type string.
2224 */
2225static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2226{
Willy Tarreau79e57332018-10-02 16:01:16 +02002227 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002228 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002229
2230 if (!args || args->type != ARGT_STR)
2231 return 0;
2232
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002233 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2234 /* HTX version */
2235 struct htx *htx = smp_prefetch_htx(smp, args);
2236 struct http_hdr_ctx ctx;
2237 struct ist hdr;
2238
2239 if (!htx)
2240 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002241
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002242 hdr = (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2243 ? ist("Cookie")
2244 : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002245
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002246 val_end = val_beg = NULL;
2247 ctx.blk = NULL;
2248 cnt = 0;
2249 while (1) {
2250 /* Note: val_beg == NULL every time we need to fetch a new header */
2251 if (!val_beg) {
2252 if (!http_find_header(htx, hdr, &ctx, 0))
2253 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002254
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002255 if (ctx.value.len < args->data.str.data + 1)
2256 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002257
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002258 val_beg = ctx.value.ptr;
2259 val_end = val_beg + ctx.value.len;
2260 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002261
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002262 smp->data.type = SMP_T_STR;
2263 smp->flags |= SMP_F_CONST;
2264 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2265 args->data.str.area, args->data.str.data,
2266 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2267 &smp->data.u.str.area,
2268 &smp->data.u.str.data))) {
2269 cnt++;
2270 }
2271 }
2272 }
2273 else {
2274 /* LEGACY version */
2275 struct http_txn *txn;
2276 struct hdr_idx *idx;
2277 struct hdr_ctx ctx;
2278 const struct http_msg *msg;
2279 const char *hdr_name;
2280 int hdr_name_len;
2281 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002282
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002283 CHECK_HTTP_MESSAGE_FIRST();
2284
2285 txn = smp->strm->txn;
2286 idx = &smp->strm->txn->hdr_idx;
2287
2288 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
2289 msg = &txn->req;
2290 hdr_name = "Cookie";
2291 hdr_name_len = 6;
2292 } else {
2293 msg = &txn->rsp;
2294 hdr_name = "Set-Cookie";
2295 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002296 }
2297
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002298 sol = ci_head(msg->chn);
2299 val_end = val_beg = NULL;
2300 ctx.idx = 0;
2301 cnt = 0;
2302
2303 while (1) {
2304 /* Note: val_beg == NULL every time we need to fetch a new header */
2305 if (!val_beg) {
2306 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2307 break;
2308
2309 if (ctx.vlen < args->data.str.data + 1)
2310 continue;
2311
2312 val_beg = ctx.line + ctx.val;
2313 val_end = val_beg + ctx.vlen;
2314 }
2315
2316 smp->data.type = SMP_T_STR;
2317 smp->flags |= SMP_F_CONST;
2318 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2319 args->data.str.area, args->data.str.data,
2320 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2321 &smp->data.u.str.area, &smp->data.u.str.data))) {
2322 cnt++;
2323 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002324 }
2325 }
2326
2327 smp->data.type = SMP_T_SINT;
2328 smp->data.u.sint = cnt;
2329 smp->flags |= SMP_F_VOL_HDR;
2330 return 1;
2331}
2332
2333/* Fetch an cookie's integer value. The integer value is returned. It
2334 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2335 */
2336static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2337{
2338 int ret = smp_fetch_cookie(args, smp, kw, private);
2339
2340 if (ret > 0) {
2341 smp->data.type = SMP_T_SINT;
2342 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2343 smp->data.u.str.data);
2344 }
2345
2346 return ret;
2347}
2348
2349/************************************************************************/
2350/* The code below is dedicated to sample fetches */
2351/************************************************************************/
2352
2353/* This scans a URL-encoded query string. It takes an optionally wrapping
2354 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2355 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2356 * pointers are updated for next iteration before leaving.
2357 */
2358static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2359{
2360 const char *vstart, *vend;
2361 struct buffer *temp;
2362 const char **chunks = (const char **)smp->ctx.a;
2363
2364 if (!http_find_next_url_param(chunks, name, name_len,
2365 &vstart, &vend, delim))
2366 return 0;
2367
2368 /* Create sample. If the value is contiguous, return the pointer as CONST,
2369 * if the value is wrapped, copy-it in a buffer.
2370 */
2371 smp->data.type = SMP_T_STR;
2372 if (chunks[2] &&
2373 vstart >= chunks[0] && vstart <= chunks[1] &&
2374 vend >= chunks[2] && vend <= chunks[3]) {
2375 /* Wrapped case. */
2376 temp = get_trash_chunk();
2377 memcpy(temp->area, vstart, chunks[1] - vstart);
2378 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2379 vend - chunks[2]);
2380 smp->data.u.str.area = temp->area;
2381 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2382 } else {
2383 /* Contiguous case. */
2384 smp->data.u.str.area = (char *)vstart;
2385 smp->data.u.str.data = vend - vstart;
2386 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2387 }
2388
2389 /* Update context, check wrapping. */
2390 chunks[0] = vend;
2391 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2392 chunks[1] = chunks[3];
2393 chunks[2] = NULL;
2394 }
2395
2396 if (chunks[0] < chunks[1])
2397 smp->flags |= SMP_F_NOT_LAST;
2398
2399 return 1;
2400}
2401
2402/* This function iterates over each parameter of the query string. It uses
2403 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2404 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2405 * An optional parameter name is passed in args[0], otherwise any parameter is
2406 * considered. It supports an optional delimiter argument for the beginning of
2407 * the string in args[1], which defaults to "?".
2408 */
2409static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2410{
Willy Tarreau79e57332018-10-02 16:01:16 +02002411 char delim = '?';
2412 const char *name;
2413 int name_len;
2414
2415 if (!args ||
2416 (args[0].type && args[0].type != ARGT_STR) ||
2417 (args[1].type && args[1].type != ARGT_STR))
2418 return 0;
2419
2420 name = "";
2421 name_len = 0;
2422 if (args->type == ARGT_STR) {
2423 name = args->data.str.area;
2424 name_len = args->data.str.data;
2425 }
2426
2427 if (args[1].type)
2428 delim = *args[1].data.str.area;
2429
2430 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002431 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2432 /* HTX version */
2433 struct htx *htx = smp_prefetch_htx(smp, args);
2434 union h1_sl sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002435
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002436 if (!htx)
2437 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002438
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002439 sl = http_find_stline(htx);
2440 smp->ctx.a[0] = http_find_param_list(sl.rq.u.ptr, sl.rq.u.len, delim);
2441 if (!smp->ctx.a[0])
2442 return 0;
2443
2444 smp->ctx.a[1] = sl.rq.u.ptr + sl.rq.u.len;
2445 }
2446 else {
2447 /* LEGACY version */
2448 struct http_msg *msg;
2449
2450 CHECK_HTTP_MESSAGE_FIRST();
2451
2452 msg = &smp->strm->txn->req;
2453
2454 smp->ctx.a[0] = http_find_param_list(ci_head(msg->chn) + msg->sl.rq.u,
2455 msg->sl.rq.u_l, delim);
2456 if (!smp->ctx.a[0])
2457 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002458
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002459 smp->ctx.a[1] = ci_head(msg->chn) + msg->sl.rq.u + msg->sl.rq.u_l;
2460 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002461
2462 /* Assume that the context is filled with NULL pointer
2463 * before the first call.
2464 * smp->ctx.a[2] = NULL;
2465 * smp->ctx.a[3] = NULL;
2466 */
2467 }
2468
2469 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2470}
2471
2472/* This function iterates over each parameter of the body. This requires
2473 * that the body has been waited for using http-buffer-request. It uses
2474 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2475 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2476 * optional second part if the body wraps at the end of the buffer. An optional
2477 * parameter name is passed in args[0], otherwise any parameter is considered.
2478 */
2479static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2480{
Willy Tarreau79e57332018-10-02 16:01:16 +02002481 const char *name;
2482 int name_len;
2483
2484 if (!args || (args[0].type && args[0].type != ARGT_STR))
2485 return 0;
2486
2487 name = "";
2488 name_len = 0;
2489 if (args[0].type == ARGT_STR) {
2490 name = args[0].data.str.area;
2491 name_len = args[0].data.str.data;
2492 }
2493
2494 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002495 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2496 /* HTX version */
2497 struct htx *htx = smp_prefetch_htx(smp, args);
2498 struct buffer *temp;
2499 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002500
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002501 if (!htx)
2502 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002503
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002504 temp = get_trash_chunk();
2505 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
2506 struct htx_blk *blk = htx_get_blk(htx, pos);
2507 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002508
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002509 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
2510 break;
2511 if (type == HTX_BLK_DATA) {
2512 if (!htx_data_to_str(htx_get_blk_value(htx, blk), temp, 0))
2513 return 0;
2514 }
2515 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002516
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002517 smp->ctx.a[0] = temp->area;
2518 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002519
2520 /* Assume that the context is filled with NULL pointer
2521 * before the first call.
2522 * smp->ctx.a[2] = NULL;
2523 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002524 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002525 }
2526 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002527 /* LEGACY version */
2528 struct http_msg *msg;
2529 unsigned long len;
2530 unsigned long block1;
2531 char *body;
2532
2533 CHECK_HTTP_MESSAGE_FIRST();
2534
2535 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2536 msg = &smp->strm->txn->req;
2537 else
2538 msg = &smp->strm->txn->rsp;
2539
2540 len = http_body_bytes(msg);
2541 body = c_ptr(msg->chn, -http_data_rewind(msg));
2542
2543 block1 = len;
2544 if (block1 > b_wrap(&msg->chn->buf) - body)
2545 block1 = b_wrap(&msg->chn->buf) - body;
2546
2547 if (block1 == len) {
2548 /* buffer is not wrapped (or empty) */
2549 smp->ctx.a[0] = body;
2550 smp->ctx.a[1] = body + len;
2551
2552 /* Assume that the context is filled with NULL pointer
2553 * before the first call.
2554 * smp->ctx.a[2] = NULL;
2555 * smp->ctx.a[3] = NULL;
2556 */
2557 }
2558 else {
2559 /* buffer is wrapped, we need to defragment it */
2560 smp->ctx.a[0] = body;
2561 smp->ctx.a[1] = body + block1;
2562 smp->ctx.a[2] = b_orig(&msg->chn->buf);
2563 smp->ctx.a[3] = b_orig(&msg->chn->buf) + ( len - block1 );
2564 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002565 }
2566 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002567
Willy Tarreau79e57332018-10-02 16:01:16 +02002568 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2569}
2570
2571/* Return the signed integer value for the specified url parameter (see url_param
2572 * above).
2573 */
2574static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2575{
2576 int ret = smp_fetch_url_param(args, smp, kw, private);
2577
2578 if (ret > 0) {
2579 smp->data.type = SMP_T_SINT;
2580 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2581 smp->data.u.str.data);
2582 }
2583
2584 return ret;
2585}
2586
2587/* This produces a 32-bit hash of the concatenation of the first occurrence of
2588 * the Host header followed by the path component if it begins with a slash ('/').
2589 * This means that '*' will not be added, resulting in exactly the first Host
2590 * entry. If no Host header is found, then the path is used. The resulting value
2591 * is hashed using the url hash followed by a full avalanche hash and provides a
2592 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2593 * high-traffic sites without having to store whole paths.
2594 * this differs from the base32 functions in that it includes the url parameters
2595 * as well as the path
2596 */
2597static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2598{
Willy Tarreau79e57332018-10-02 16:01:16 +02002599 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002600
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002601 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2602 /* HTX version */
2603 struct htx *htx = smp_prefetch_htx(smp, args);
2604 struct http_hdr_ctx ctx;
2605 union h1_sl sl;
2606 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002607
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002608 if (!htx)
2609 return 0;
2610
2611 ctx.blk = NULL;
2612 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2613 /* OK we have the header value in ctx.value */
2614 while (ctx.value.len--)
2615 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2616 }
2617
2618 /* now retrieve the path */
2619 sl = http_find_stline(htx);
2620 path = http_get_path(sl.rq.u);
2621 while (path.len > 0 && *(path.ptr) != '?') {
2622 path.ptr++;
2623 path.len--;
2624 }
2625 if (path.len && *(path.ptr) == '/') {
2626 while (path.len--)
2627 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2628 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002629 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002630 else {
2631 /* LEGACY version */
2632 struct http_txn *txn;
2633 struct hdr_ctx ctx;
2634 char *ptr, *beg, *end;
2635 int len;
2636
2637 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02002638
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002639 txn = smp->strm->txn;
2640 ctx.idx = 0;
2641 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
2642 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2643 ptr = ctx.line + ctx.val;
2644 len = ctx.vlen;
2645 while (len--)
2646 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2647 }
2648
2649 /* now retrieve the path */
2650 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
2651 beg = http_txn_get_path(txn);
2652 if (!beg)
2653 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002654
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002655 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002656
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002657 if (beg < ptr && *beg == '/') {
2658 while (beg < ptr)
2659 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2660 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002661 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002662
Willy Tarreau79e57332018-10-02 16:01:16 +02002663 hash = full_hash(hash);
2664
2665 smp->data.type = SMP_T_SINT;
2666 smp->data.u.sint = hash;
2667 smp->flags = SMP_F_VOL_1ST;
2668 return 1;
2669}
2670
2671/* This concatenates the source address with the 32-bit hash of the Host and
2672 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2673 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2674 * on the source address length. The URL hash is stored before the address so
2675 * that in environments where IPv6 is insignificant, truncating the output to
2676 * 8 bytes would still work.
2677 */
2678static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2679{
2680 struct buffer *temp;
2681 struct connection *cli_conn = objt_conn(smp->sess->origin);
2682
2683 if (!cli_conn)
2684 return 0;
2685
2686 if (!smp_fetch_url32(args, smp, kw, private))
2687 return 0;
2688
2689 temp = get_trash_chunk();
2690 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2691 temp->data += sizeof(unsigned int);
2692
2693 switch (cli_conn->addr.from.ss_family) {
2694 case AF_INET:
2695 memcpy(temp->area + temp->data,
2696 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2697 4);
2698 temp->data += 4;
2699 break;
2700 case AF_INET6:
2701 memcpy(temp->area + temp->data,
2702 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2703 16);
2704 temp->data += 16;
2705 break;
2706 default:
2707 return 0;
2708 }
2709
2710 smp->data.u.str = *temp;
2711 smp->data.type = SMP_T_BIN;
2712 return 1;
2713}
2714
2715/************************************************************************/
2716/* Other utility functions */
2717/************************************************************************/
2718
2719/* This function is used to validate the arguments passed to any "hdr" fetch
2720 * keyword. These keywords support an optional positive or negative occurrence
2721 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2722 * is assumed that the types are already the correct ones. Returns 0 on error,
2723 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2724 * error message in case of error, that the caller is responsible for freeing.
2725 * The initial location must either be freeable or NULL.
2726 * Note: this function's pointer is checked from Lua.
2727 */
2728int val_hdr(struct arg *arg, char **err_msg)
2729{
2730 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2731 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2732 return 0;
2733 }
2734 return 1;
2735}
2736
2737/************************************************************************/
2738/* All supported sample fetch keywords must be declared here. */
2739/************************************************************************/
2740
2741/* Note: must not be declared <const> as its list will be overwritten */
2742static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2743 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2744 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2745 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2746
2747 /* capture are allocated and are permanent in the stream */
2748 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2749
2750 /* retrieve these captures from the HTTP logs */
2751 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2752 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2753 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2754
2755 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2756 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2757
2758 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2759 * are only here to match the ACL's name, are request-only and are used
2760 * for ACL compatibility only.
2761 */
2762 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2763 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
2764 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2765 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2766
2767 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2768 * only here to match the ACL's name, are request-only and are used for
2769 * ACL compatibility only.
2770 */
2771 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
2772 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2773 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2774 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2775
2776 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2777 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2778 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2779 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2780 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2781 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2782
2783 /* HTTP protocol on the request path */
2784 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2785 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2786
2787 /* HTTP version on the request path */
2788 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2789 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2790
2791 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2792 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2793 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2794 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2795
2796 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2797 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2798
2799 /* HTTP version on the response path */
2800 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2801 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2802
2803 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2804 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2805 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2806 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2807
2808 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2809 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2810 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2811 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2812 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2813 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2814 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2815
2816 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2817 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2818 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2819 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2820
2821 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2822 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2823 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2824 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2825 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2826 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2827 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2828
2829 /* scook is valid only on the response and is used for ACL compatibility */
2830 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2831 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2832 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2833 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2834
2835 /* shdr is valid only on the response and is used for ACL compatibility */
2836 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2837 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2838 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2839 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2840
2841 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2842 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2843 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2844 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2845 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2846 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2847 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2848 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2849 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2850 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2851 { /* END */ },
2852}};
2853
2854
2855__attribute__((constructor))
2856static void __http_fetch_init(void)
2857{
2858 sample_register_fetches(&sample_fetch_keywords);
2859}
2860
2861/*
2862 * Local variables:
2863 * c-indent-level: 8
2864 * c-basic-offset: 8
2865 * End:
2866 */