blob: 292da21873d9cf8956eb78a04dfae5a49503e7b7 [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
2 * HTTP samples fetching
3 *
4 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/types.h>
14
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
18
19#include <common/base64.h>
20#include <common/chunk.h>
21#include <common/compat.h>
22#include <common/config.h>
23#include <common/debug.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010024#include <common/h1.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020025#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010026#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010027#include <common/initcall.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020028#include <common/memory.h>
29#include <common/standard.h>
30#include <common/version.h>
31
32#include <types/global.h>
33
34#include <proto/arg.h>
35#include <proto/auth.h>
Willy Tarreau538746a2018-12-11 10:59:20 +010036#include <proto/hdr_idx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020037#include <proto/http_fetch.h>
Christopher Fauletef453ed2018-10-24 21:39:27 +020038#include <proto/http_htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020039#include <proto/log.h>
40#include <proto/obj_type.h>
41#include <proto/proto_http.h>
42#include <proto/sample.h>
43#include <proto/stream.h>
44
45
46/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
47static THREAD_LOCAL struct hdr_ctx static_hdr_ctx;
Christopher Fauletef453ed2018-10-24 21:39:27 +020048static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
49
Willy Tarreau79e57332018-10-02 16:01:16 +020050
51/*
52 * Returns the data from Authorization header. Function may be called more
53 * than once so data is stored in txn->auth_data. When no header is found
54 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
55 * searching again for something we are unable to find anyway. However, if
56 * the result if valid, the cache is not reused because we would risk to
57 * have the credentials overwritten by another stream in parallel.
58 */
59
Christopher Faulet311c7ea2018-10-24 21:41:55 +020060static int get_http_auth(struct sample *smp)
Willy Tarreau79e57332018-10-02 16:01:16 +020061{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020062 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020063 struct http_txn *txn = s->txn;
64 struct buffer auth_method;
Willy Tarreau79e57332018-10-02 16:01:16 +020065 char *h, *p;
66 int len;
67
68#ifdef DEBUG_AUTH
69 printf("Auth for stream %p: %d\n", s, txn->auth.method);
70#endif
Willy Tarreau79e57332018-10-02 16:01:16 +020071 if (txn->auth.method == HTTP_AUTH_WRONG)
72 return 0;
73
74 txn->auth.method = HTTP_AUTH_WRONG;
75
Christopher Faulet311c7ea2018-10-24 21:41:55 +020076 if (IS_HTX_STRM(s) || (smp->px->mode == PR_MODE_TCP)) {
77 /* HTX version */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010078 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet311c7ea2018-10-24 21:41:55 +020079 struct http_hdr_ctx ctx = { .blk = NULL };
80 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020081
Christopher Faulet311c7ea2018-10-24 21:41:55 +020082 if (txn->flags & TX_USE_PX_CONN)
83 hdr = ist("Proxy-Authorization");
84 else
85 hdr = ist("Authorization");
86
Christopher Faulet311c7ea2018-10-24 21:41:55 +020087 ctx.blk = NULL;
88 if (!http_find_header(htx, hdr, &ctx, 0))
89 return 0;
90
91 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
92 len = p - ctx.value.ptr;
93 if (!p || len <= 0)
94 return 0;
95
96 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
97 return 0;
98
99 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200100 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200101 else {
102 /* LEGACY version */
103 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau79e57332018-10-02 16:01:16 +0200104
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200105 if (txn->flags & TX_USE_PX_CONN) {
106 h = "Proxy-Authorization";
107 len = strlen(h);
108 } else {
109 h = "Authorization";
110 len = strlen(h);
111 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200112
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200113 if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx))
114 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200115
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200116 h = ctx.line + ctx.val;
Willy Tarreau79e57332018-10-02 16:01:16 +0200117
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200118 p = memchr(h, ' ', ctx.vlen);
119 len = p - h;
120 if (!p || len <= 0)
121 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200122
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200123 if (chunk_initlen(&auth_method, h, 0, len) != 1)
124 return 0;
125
126 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
127 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200128
129 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
130 struct buffer *http_auth = get_trash_chunk();
131
132 len = base64dec(txn->auth.method_data.area,
133 txn->auth.method_data.data,
134 http_auth->area, global.tune.bufsize - 1);
135
136 if (len < 0)
137 return 0;
138
139
140 http_auth->area[len] = '\0';
141
142 p = strchr(http_auth->area, ':');
143
144 if (!p)
145 return 0;
146
147 txn->auth.user = http_auth->area;
148 *p = '\0';
149 txn->auth.pass = p+1;
150
151 txn->auth.method = HTTP_AUTH_BASIC;
152 return 1;
153 }
154
155 return 0;
156}
157
158/* This function ensures that the prerequisites for an L7 fetch are ready,
159 * which means that a request or response is ready. If some data is missing,
160 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Fauletef453ed2018-10-24 21:39:27 +0200161 * to extract data from L7.
162 *
163 * The function returns :
164 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
165 * decide whether or not an HTTP message is present ;
166 * NULL if the requested data cannot be fetched or if it is certain that
167 * we'll never have any HTTP message there ;
168 * The HTX message if ready
169 */
170struct htx *smp_prefetch_htx(struct sample *smp, const struct arg *args)
171{
172 struct proxy *px = smp->px;
173 struct stream *s = smp->strm;
174 unsigned int opt = smp->opt;
175 struct http_txn *txn = NULL;
176 struct htx *htx = NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100177 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200178
179 /* Note: it is possible that <s> is NULL when called before stream
180 * initialization (eg: tcp-request connection), so this function is the
181 * one responsible for guarding against this case for all HTTP users.
182 */
183 if (!s)
184 return NULL;
185
186 if (!s->txn) {
187 if (unlikely(!http_alloc_txn(s)))
188 return NULL; /* not enough memory */
189 http_init_txn(s);
190 txn = s->txn;
191 }
192
193 if (px->mode == PR_MODE_HTTP) {
194 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100195 htx = htxbuf(&s->req.buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200196 if (htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH) {
197 /* Parsing is done by the mux, just wait */
198 smp->flags |= SMP_F_MAY_CHANGE;
199 return NULL;
200 }
201
202 /* OK we just got a valid HTTP request. We have some
203 * minor preparation to perform so that further checks
204 * can rely on HTTP tests.
205 */
206 if (txn) {
207 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100208 txn->meth = sl->info.req.meth;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200209 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
210 s->flags |= SF_REDIRECTABLE;
211 }
212
213 /* otherwise everything's ready for the request */
214 }
215 else {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100216 htx = htxbuf(&s->res.buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200217 if (htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH) {
218 /* Parsing is done by the mux, just wait */
219 smp->flags |= SMP_F_MAY_CHANGE;
220 return NULL;
221 }
222 }
223 }
224 else { /* PR_MODE_TCP */
225 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
226 struct buffer *buf;
227 struct h1m h1m;
228 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100229 union h1_sl h1sl;
230 unsigned int flags = HTX_FL_NONE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200231 int ret;
232
233 buf = &s->req.buf;
234 if (b_head(buf) + b_data(buf) > b_wrap(buf))
235 b_slow_realign(buf, trash.area, 0);
236
237 h1m_init_req(&h1m);
238 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100239 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200240 if (ret <= 0) {
241 /* Invalid or too big*/
242 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
243 return NULL;
244
245 /* wait for a full request */
246 smp->flags |= SMP_F_MAY_CHANGE;
247 return NULL;
248 }
249
250 /* OK we just got a valid HTTP request. We have to
251 * convert it into an HTX message.
252 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100253 if (unlikely(h1sl.rq.v.len == 0)) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200254 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100255 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200256 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100257 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200258 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100259 else if ((h1sl.rq.v.len == 8) &&
260 ((*(h1sl.rq.v.ptr + 5) > '1') ||
261 ((*(h1sl.rq.v.ptr + 5) == '1') && (*(h1sl.rq.v.ptr + 7) >= '1'))))
262 h1m.flags |= H1_MF_VER_11;
263
264
265 /* Set HTX start-line flags */
266 if (h1m.flags & H1_MF_VER_11)
267 flags |= HTX_SL_F_VER_11;
268 if (h1m.flags & H1_MF_XFER_ENC)
269 flags |= HTX_SL_F_XFER_ENC;
270 if (h1m.flags & H1_MF_XFER_LEN) {
271 flags |= HTX_SL_F_XFER_LEN;
272 if (h1m.flags & H1_MF_CHNK)
273 flags |= HTX_SL_F_CHNK;
274 else if (h1m.flags & H1_MF_CLEN)
275 flags |= HTX_SL_F_CLEN;
276 }
277
Christopher Fauletef453ed2018-10-24 21:39:27 +0200278 htx = htx_from_buf(get_trash_chunk());
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100279 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
280 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200281 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100282 sl->info.req.meth = h1sl.rq.meth;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200283
284 if (txn) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100285 txn->meth = h1sl.rq.meth;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200286 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
287 s->flags |= SF_REDIRECTABLE;
288 }
289 /* Ok, now everything's ready for the request */
290 }
291 else {
292 /* Impossible, no HTTP fetch on tcp-response */
293 return NULL;
294 }
295 }
296
297 /* everything's OK */
298 smp->data.u.sint = 1;
299 return htx;
300}
301
302/* This function ensures that the prerequisites for an L7 fetch are ready,
303 * which means that a request or response is ready. If some data is missing,
304 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau79e57332018-10-02 16:01:16 +0200305 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
306 * another test is made to ensure the required information is not gone.
307 *
308 * The function returns :
309 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
310 * decide whether or not an HTTP message is present ;
311 * 0 if the requested data cannot be fetched or if it is certain that
312 * we'll never have any HTTP message there ;
313 * 1 if an HTTP message is ready
314 */
315int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
316 const struct arg *args, struct sample *smp, int req_vol)
317{
318 struct http_txn *txn;
319 struct http_msg *msg;
320
321 /* Note: it is possible that <s> is NULL when called before stream
322 * initialization (eg: tcp-request connection), so this function is the
323 * one responsible for guarding against this case for all HTTP users.
324 */
325 if (!s)
326 return 0;
327
328 if (!s->txn) {
329 if (unlikely(!http_alloc_txn(s)))
330 return 0; /* not enough memory */
331 http_init_txn(s);
332 }
333 txn = s->txn;
334 msg = &txn->req;
335
336 /* Check for a dependency on a request */
337 smp->data.type = SMP_T_BOOL;
338
339 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
340 /* If the buffer does not leave enough free space at the end,
341 * we must first realign it.
342 */
343 if (ci_head(&s->req) > b_orig(&s->req.buf) &&
344 ci_head(&s->req) + ci_data(&s->req) > b_wrap(&s->req.buf) - global.tune.maxrewrite)
345 channel_slow_realign(&s->req, trash.area);
346
347 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
348 if (msg->msg_state == HTTP_MSG_ERROR)
349 return 0;
350
351 /* Try to decode HTTP request */
352 if (likely(msg->next < ci_data(&s->req)))
353 http_msg_analyzer(msg, &txn->hdr_idx);
354
355 /* Still no valid request ? */
356 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
357 if ((msg->msg_state == HTTP_MSG_ERROR) ||
358 channel_full(&s->req, global.tune.maxrewrite)) {
359 return 0;
360 }
361 /* wait for final state */
362 smp->flags |= SMP_F_MAY_CHANGE;
363 return 0;
364 }
365
366 /* OK we just got a valid HTTP request. We have some minor
367 * preparation to perform so that further checks can rely
368 * on HTTP tests.
369 */
370
371 /* If the request was parsed but was too large, we must absolutely
372 * return an error so that it is not processed. At the moment this
373 * cannot happen, but if the parsers are to change in the future,
374 * we want this check to be maintained.
375 */
376 if (unlikely(ci_head(&s->req) + ci_data(&s->req) >
377 b_wrap(&s->req.buf) - global.tune.maxrewrite)) {
378 msg->err_state = msg->msg_state;
379 msg->msg_state = HTTP_MSG_ERROR;
380 smp->data.u.sint = 1;
381 return 1;
382 }
383
384 txn->meth = find_http_meth(ci_head(msg->chn), msg->sl.rq.m_l);
385 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
386 s->flags |= SF_REDIRECTABLE;
387
388 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
389 return 0;
390 }
391
392 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
393 return 0; /* data might have moved and indexes changed */
394 }
395
396 /* otherwise everything's ready for the request */
397 }
398 else {
399 /* Check for a dependency on a response */
400 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
401 smp->flags |= SMP_F_MAY_CHANGE;
402 return 0;
403 }
404 }
405
406 /* everything's OK */
407 smp->data.u.sint = 1;
408 return 1;
409}
410
411/* This function fetches the method of current HTTP request and stores
412 * it in the global pattern struct as a chunk. There are two possibilities :
413 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
414 * in <len> and <ptr> is NULL ;
415 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
416 * <len> to its length.
417 * This is intended to be used with pat_match_meth() only.
418 */
419static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
420{
421 int meth;
422 struct http_txn *txn;
423
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200424 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
425 /* HTX version */
426 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +0200427
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200428 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +0200429 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200430
431 txn = smp->strm->txn;
432 meth = txn->meth;
433 smp->data.type = SMP_T_METH;
434 smp->data.u.meth.meth = meth;
435 if (meth == HTTP_METH_OTHER) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100436 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200437
438 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
439 /* ensure the indexes are not affected */
440 return 0;
441
442 sl = http_find_stline(htx);
443 smp->flags |= SMP_F_CONST;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100444 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
445 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200446 }
447 smp->flags |= SMP_F_VOL_1ST;
448 }
449 else {
450 /* LEGACY version */
451 CHECK_HTTP_MESSAGE_FIRST_PERM();
452
453 txn = smp->strm->txn;
454 meth = txn->meth;
455 smp->data.type = SMP_T_METH;
456 smp->data.u.meth.meth = meth;
457 if (meth == HTTP_METH_OTHER) {
458 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
459 /* ensure the indexes are not affected */
460 return 0;
461 smp->flags |= SMP_F_CONST;
462 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
463 smp->data.u.meth.str.area = ci_head(txn->req.chn);
464 }
465 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200466 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200467 return 1;
468}
469
470static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
471{
472 struct http_txn *txn;
473 char *ptr;
474 int len;
475
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200476 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
477 /* HTX version */
478 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100479 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200480
481 if (!htx)
482 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200483
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200484 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100485 len = HTX_SL_REQ_VLEN(sl);
486 ptr = HTX_SL_REQ_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200487 }
488 else {
489 /* LEGACY version */
490 CHECK_HTTP_MESSAGE_FIRST();
491
492 txn = smp->strm->txn;
493 len = txn->req.sl.rq.v_l;
494 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.v;
495 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200496
497 while ((len-- > 0) && (*ptr++ != '/'));
498 if (len <= 0)
499 return 0;
500
501 smp->data.type = SMP_T_STR;
502 smp->data.u.str.area = ptr;
503 smp->data.u.str.data = len;
504
505 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
506 return 1;
507}
508
509static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
510{
511 struct http_txn *txn;
512 char *ptr;
513 int len;
514
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200515 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
516 /* HTX version */
517 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100518 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200519
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200520 if (!htx)
521 return 0;
522
523 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100524 len = HTX_SL_RES_VLEN(sl);
525 ptr = HTX_SL_RES_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200526 }
527 else {
528 /* LEGACY version */
529 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +0200530
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200531 txn = smp->strm->txn;
532 if (txn->rsp.msg_state < HTTP_MSG_BODY)
533 return 0;
534
535 len = txn->rsp.sl.st.v_l;
536 ptr = ci_head(txn->rsp.chn);
537 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200538
539 while ((len-- > 0) && (*ptr++ != '/'));
540 if (len <= 0)
541 return 0;
542
543 smp->data.type = SMP_T_STR;
544 smp->data.u.str.area = ptr;
545 smp->data.u.str.data = len;
546
547 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
548 return 1;
549}
550
551/* 3. Check on Status Code. We manipulate integers here. */
552static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
553{
554 struct http_txn *txn;
555 char *ptr;
556 int len;
557
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200558 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
559 /* HTX version */
560 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100561 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200562
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200563 if (!htx)
564 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200565
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200566 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100567 len = HTX_SL_RES_CLEN(sl);
568 ptr = HTX_SL_RES_CPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200569 }
570 else {
571 /* LEGACY version */
572 CHECK_HTTP_MESSAGE_FIRST();
573
574 txn = smp->strm->txn;
575 if (txn->rsp.msg_state < HTTP_MSG_BODY)
576 return 0;
577
578 len = txn->rsp.sl.st.c_l;
579 ptr = ci_head(txn->rsp.chn) + txn->rsp.sl.st.c;
580 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200581
582 smp->data.type = SMP_T_SINT;
583 smp->data.u.sint = __strl2ui(ptr, len);
584 smp->flags = SMP_F_VOL_1ST;
585 return 1;
586}
587
588static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
589{
590 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
591 return 0;
592
593 if (!smp->strm->unique_id) {
594 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
595 return 0;
596 smp->strm->unique_id[0] = '\0';
597 }
598 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
599 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
600
601 smp->data.type = SMP_T_STR;
602 smp->data.u.str.area = smp->strm->unique_id;
603 smp->flags = SMP_F_CONST;
604 return 1;
605}
606
607/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800608 * empty line which separes headers from the body. This is useful
609 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200610 */
611static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
612{
Willy Tarreau79e57332018-10-02 16:01:16 +0200613 struct http_txn *txn;
614
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200615 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
616 /* HTX version */
617 struct htx *htx = smp_prefetch_htx(smp, args);
618 struct buffer *temp;
619 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200620
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200621 if (!htx)
622 return 0;
623 temp = get_trash_chunk();
624 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
625 struct htx_blk *blk = htx_get_blk(htx, pos);
626 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200627
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200628 if (type == HTX_BLK_HDR) {
629 struct ist n = htx_get_blk_name(htx, blk);
630 struct ist v = htx_get_blk_value(htx, blk);
631
Christopher Fauletc59ff232018-12-03 13:58:44 +0100632 if (!htx_hdr_to_h1(n, v, temp))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200633 return 0;
634 }
635 else if (type == HTX_BLK_EOH) {
636 if (!chunk_memcat(temp, "\r\n", 2))
637 return 0;
638 break;
639 }
640 }
641 smp->data.type = SMP_T_STR;
642 smp->data.u.str = *temp;
643
644 }
645 else {
646 /* LEGACY version */
647 struct http_msg *msg;
648 struct hdr_idx *idx;
649
650 CHECK_HTTP_MESSAGE_FIRST();
651
652 txn = smp->strm->txn;
653 idx = &txn->hdr_idx;
654 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200655
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200656 smp->data.type = SMP_T_STR;
657 smp->data.u.str.area = ci_head(msg->chn) + hdr_idx_first_pos(idx);
658 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
659 (ci_head(msg->chn)[msg->eoh] == '\r');
660 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200661 return 1;
662}
663
664/* Returns the header request in a length/value encoded format.
665 * This is useful for exchanges with the SPOE.
666 *
667 * A "length value" is a multibyte code encoding numbers. It uses the
668 * SPOE format. The encoding is the following:
669 *
670 * Each couple "header name" / "header value" is composed
671 * like this:
672 * "length value" "header name bytes"
673 * "length value" "header value bytes"
674 * When the last header is reached, the header name and the header
675 * value are empty. Their length are 0
676 */
677static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
678{
Willy Tarreau79e57332018-10-02 16:01:16 +0200679 struct http_txn *txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200680 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200681
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200682 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
683 /* HTX version */
684 struct htx *htx = smp_prefetch_htx(smp, args);
685 struct buffer *temp;
686 char *p, *end;
687 int32_t pos;
688 int ret;
Willy Tarreau79e57332018-10-02 16:01:16 +0200689
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200690 if (!htx)
691 return 0;
692 temp = get_trash_chunk();
693 p = temp->area;
694 end = temp->area + temp->size;
695 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
696 struct htx_blk *blk = htx_get_blk(htx, pos);
697 enum htx_blk_type type = htx_get_blk_type(blk);
698 struct ist n, v;
Willy Tarreau79e57332018-10-02 16:01:16 +0200699
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200700 if (type == HTX_BLK_HDR) {
701 n = htx_get_blk_name(htx,blk);
702 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200703
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200704 /* encode the header name. */
705 ret = encode_varint(n.len, &p, end);
706 if (ret == -1)
707 return 0;
708 if (p + n.len > end)
709 return 0;
710 memcpy(p, n.ptr, n.len);
711 p += n.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200712
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200713 /* encode the header value. */
714 ret = encode_varint(v.len, &p, end);
715 if (ret == -1)
716 return 0;
717 if (p + v.len > end)
718 return 0;
719 memcpy(p, v.ptr, v.len);
720 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200721
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200722 }
723 else if (type == HTX_BLK_EOH) {
724 /* encode the end of the header list with empty
725 * header name and header value.
726 */
727 ret = encode_varint(0, &p, end);
728 if (ret == -1)
729 return 0;
730 ret = encode_varint(0, &p, end);
731 if (ret == -1)
732 return 0;
733 break;
734 }
735 }
736
737 /* Initialise sample data which will be filled. */
738 smp->data.type = SMP_T_BIN;
739 smp->data.u.str.area = temp->area;
740 smp->data.u.str.data = p - temp->area;
741 smp->data.u.str.size = temp->size;
742 }
743 else {
744 /* LEGACY version */
745 struct http_msg *msg;
746 struct hdr_idx *idx;
747 const char *cur_ptr, *cur_next, *p;
748 int old_idx, cur_idx;
749 struct hdr_idx_elem *cur_hdr;
750 const char *hn, *hv;
751 int hnl, hvl;
752 int ret;
753 char *buf;
754 char *end;
755
756 CHECK_HTTP_MESSAGE_FIRST();
757
758 temp = get_trash_chunk();
759 buf = temp->area;
760 end = temp->area + temp->size;
761
762 txn = smp->strm->txn;
763 idx = &txn->hdr_idx;
764 msg = &txn->req;
765
766 /* Build array of headers. */
767 old_idx = 0;
768 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(idx);
769 while (1) {
770 cur_idx = idx->v[old_idx].next;
771 if (!cur_idx)
772 break;
773 old_idx = cur_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +0200774
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200775 cur_hdr = &idx->v[cur_idx];
776 cur_ptr = cur_next;
777 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
778
779 /* Now we have one full header at cur_ptr of len cur_hdr->len,
780 * and the next header starts at cur_next. We'll check
781 * this header in the list as well as against the default
782 * rule.
783 */
784
785 /* look for ': *'. */
786 hn = cur_ptr;
787 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
788 if (p >= cur_ptr+cur_hdr->len)
789 continue;
790 hnl = p - hn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200791 p++;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200792 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
793 p++;
794 if (p >= cur_ptr + cur_hdr->len)
795 continue;
796 hv = p;
797 hvl = cur_ptr + cur_hdr->len-p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200798
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200799 /* encode the header name. */
800 ret = encode_varint(hnl, &buf, end);
801 if (ret == -1)
802 return 0;
803 if (buf + hnl > end)
804 return 0;
805 memcpy(buf, hn, hnl);
806 buf += hnl;
807
808 /* encode and copy the value. */
809 ret = encode_varint(hvl, &buf, end);
810 if (ret == -1)
811 return 0;
812 if (buf + hvl > end)
813 return 0;
814 memcpy(buf, hv, hvl);
815 buf += hvl;
816 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200817
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200818 /* encode the end of the header list with empty
819 * header name and header value.
820 */
821 ret = encode_varint(0, &buf, end);
Willy Tarreau79e57332018-10-02 16:01:16 +0200822 if (ret == -1)
823 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200824 ret = encode_varint(0, &buf, end);
825 if (ret == -1)
Willy Tarreau79e57332018-10-02 16:01:16 +0200826 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200827
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200828 /* Initialise sample data which will be filled. */
829 smp->data.type = SMP_T_BIN;
830 smp->data.u.str.area = temp->area;
831 smp->data.u.str.data = buf - temp->area;
832 smp->data.u.str.size = temp->size;
833 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200834 return 1;
835}
836
837/* returns the longest available part of the body. This requires that the body
838 * has been waited for using http-buffer-request.
839 */
840static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
841{
Willy Tarreau79e57332018-10-02 16:01:16 +0200842 struct buffer *temp;
843
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200844 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
845 /* HTX version */
846 struct htx *htx = smp_prefetch_htx(smp, args);
847 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200848
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200849 if (!htx)
850 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200851
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200852 temp = get_trash_chunk();
853 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
854 struct htx_blk *blk = htx_get_blk(htx, pos);
855 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200856
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200857 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
858 break;
859 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +0100860 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200861 return 0;
862 }
863 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200864
Willy Tarreau79e57332018-10-02 16:01:16 +0200865 smp->data.type = SMP_T_BIN;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200866 smp->data.u.str = *temp;
867 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200868 }
869 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200870 /* LEGACY version */
871 struct http_msg *msg;
872 unsigned long len;
873 unsigned long block1;
874 char *body;
875
876 CHECK_HTTP_MESSAGE_FIRST();
877
878 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
879 msg = &smp->strm->txn->req;
880 else
881 msg = &smp->strm->txn->rsp;
882
883 len = http_body_bytes(msg);
884 body = c_ptr(msg->chn, -http_data_rewind(msg));
885
886 block1 = len;
887 if (block1 > b_wrap(&msg->chn->buf) - body)
888 block1 = b_wrap(&msg->chn->buf) - body;
889
890 if (block1 == len) {
891 /* buffer is not wrapped (or empty) */
892 smp->data.type = SMP_T_BIN;
893 smp->data.u.str.area = body;
894 smp->data.u.str.data = len;
895 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
896 }
897 else {
898 /* buffer is wrapped, we need to defragment it */
899 temp = get_trash_chunk();
900 memcpy(temp->area, body, block1);
901 memcpy(temp->area + block1, b_orig(&msg->chn->buf),
902 len - block1);
903 smp->data.type = SMP_T_BIN;
904 smp->data.u.str.area = temp->area;
905 smp->data.u.str.data = len;
906 smp->flags = SMP_F_VOL_TEST;
907 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200908 }
909 return 1;
910}
911
912
913/* returns the available length of the body. This requires that the body
914 * has been waited for using http-buffer-request.
915 */
916static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
917{
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200918 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
919 /* HTX version */
Christopher Fauletc16317d2018-12-12 14:11:22 +0100920 struct htx *htx = smp_prefetch_htx(smp, args);
921 struct htx_blk *blk;
922 unsigned long long len = 0;
923
924 if (!htx)
925 return 0;
926
927 len = htx->data;
928
929 /* Remove the length of headers part */
930 blk = htx_get_head_blk(htx);
931 while (blk) {
932 len -= htx_get_blksz(blk);
933 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
934 break;
935 blk = htx_get_next_blk(htx, blk);
936 }
937
938 smp->data.type = SMP_T_SINT;
939 smp->data.u.sint = len;
940
941 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200942 }
943 else {
944 /* LEGACY version */
945 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200946
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200947 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +0200948
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200949 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
950 msg = &smp->strm->txn->req;
951 else
952 msg = &smp->strm->txn->rsp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200953
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200954 smp->data.type = SMP_T_SINT;
955 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreau79e57332018-10-02 16:01:16 +0200956
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200957 smp->flags = SMP_F_VOL_TEST;
958 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200959 return 1;
960}
961
962
963/* returns the advertised length of the body, or the advertised size of the
964 * chunks available in the buffer. This requires that the body has been waited
965 * for using http-buffer-request.
966 */
967static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
968{
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200969 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
970 /* HTX version */
Christopher Fauletc16317d2018-12-12 14:11:22 +0100971 struct htx *htx = smp_prefetch_htx(smp, args);
972 struct htx_blk *blk;
973 unsigned long long len = 0;
974
975 if (!htx)
976 return 0;
977
978 len = htx->data;
979
980 /* Remove the length of headers part */
981 blk = htx_get_head_blk(htx);
982 while (blk) {
983 len -= htx_get_blksz(blk);
984 if (htx_get_blk_type(blk) == HTX_BLK_EOH)
985 break;
986 blk = htx_get_next_blk(htx, blk);
987 }
988 if (htx->extra != ULLONG_MAX)
989 len += htx->extra;
990
991 smp->data.type = SMP_T_SINT;
992 smp->data.u.sint = len;
993
994 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200995 }
996 else {
997 /* LEGACY version */
998 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200999
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001000 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001001
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001002 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
1003 msg = &smp->strm->txn->req;
1004 else
1005 msg = &smp->strm->txn->rsp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001006
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001007 smp->data.type = SMP_T_SINT;
1008 smp->data.u.sint = msg->body_len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001009
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001010 smp->flags = SMP_F_VOL_TEST;
1011 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001012 return 1;
1013}
1014
1015
1016/* 4. Check on URL/URI. A pointer to the URI is stored. */
1017static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
1018{
1019 struct http_txn *txn;
1020
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001021 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1022 /* HTX version */
1023 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001024 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001025
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001026 if (!htx)
1027 return 0;
1028 sl = http_find_stline(htx);
1029 smp->data.type = SMP_T_STR;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001030 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
1031 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001032 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1033 }
1034 else {
1035 /* LEGACY version */
1036 CHECK_HTTP_MESSAGE_FIRST();
1037
1038 txn = smp->strm->txn;
1039 smp->data.type = SMP_T_STR;
1040 smp->data.u.str.data = txn->req.sl.rq.u_l;
1041 smp->data.u.str.area = ci_head(txn->req.chn) + txn->req.sl.rq.u;
1042 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1043 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001044 return 1;
1045}
1046
1047static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1048{
1049 struct http_txn *txn;
1050 struct sockaddr_storage addr;
1051
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001052 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1053 /* HTX version */
1054 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001055 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001056
1057 if (!htx)
1058 return 0;
1059 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001060 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001061 }
1062 else {
1063 /* LEGACY version */
1064 CHECK_HTTP_MESSAGE_FIRST();
1065
1066 txn = smp->strm->txn;
1067 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
1068 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001069
Willy Tarreau79e57332018-10-02 16:01:16 +02001070 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1071 return 0;
1072
1073 smp->data.type = SMP_T_IPV4;
1074 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
1075 smp->flags = 0;
1076 return 1;
1077}
1078
1079static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
1080{
1081 struct http_txn *txn;
1082 struct sockaddr_storage addr;
1083
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001084 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1085 /* HTX version */
1086 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001087 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001088
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001089 if (!htx)
1090 return 0;
1091 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001092 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001093 }
1094 else {
1095 /* LEGACY version */
1096 CHECK_HTTP_MESSAGE_FIRST();
1097
1098 txn = smp->strm->txn;
1099 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
1100 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001101 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1102 return 0;
1103
1104 smp->data.type = SMP_T_SINT;
1105 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
1106 smp->flags = 0;
1107 return 1;
1108}
1109
1110/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1111 * Accepts an optional argument of type string containing the header field name,
1112 * and an optional argument of type signed or unsigned integer to request an
1113 * explicit occurrence of the header. Note that in the event of a missing name,
1114 * headers are considered from the first one. It does not stop on commas and
1115 * returns full lines instead (useful for User-Agent or Date for example).
1116 */
1117static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1118{
Willy Tarreau79e57332018-10-02 16:01:16 +02001119 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001120
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001121 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1122 /* HTX version */
1123 struct htx *htx = smp_prefetch_htx(smp, args);
1124 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1125 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +02001126
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001127 if (!ctx) {
1128 /* first call */
1129 ctx = &static_http_hdr_ctx;
1130 ctx->blk = NULL;
1131 smp->ctx.a[0] = ctx;
1132 }
1133
1134 if (args) {
1135 if (args[0].type != ARGT_STR)
1136 return 0;
1137 name.ptr = args[0].data.str.area;
1138 name.len = args[0].data.str.data;
1139
1140 if (args[1].type == ARGT_SINT)
1141 occ = args[1].data.sint;
1142 }
1143
1144 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001145 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001146
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001147 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1148 /* search for header from the beginning */
1149 ctx->blk = NULL;
1150
1151 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1152 /* no explicit occurrence and single fetch => last header by default */
1153 occ = -1;
1154
1155 if (!occ)
1156 /* prepare to report multiple occurrences for ACL fetches */
1157 smp->flags |= SMP_F_NOT_LAST;
1158
1159 smp->data.type = SMP_T_STR;
1160 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1161 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1162 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001163 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001164 else {
1165 /* LEGACY version */
1166 struct hdr_idx *idx;
1167 struct hdr_ctx *ctx = smp->ctx.a[0];
1168 const struct http_msg *msg;
1169 const char *name_str = NULL;
1170 int name_len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001171
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001172 if (!ctx) {
1173 /* first call */
1174 ctx = &static_hdr_ctx;
1175 ctx->idx = 0;
1176 smp->ctx.a[0] = ctx;
1177 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001178
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001179 if (args) {
1180 if (args[0].type != ARGT_STR)
1181 return 0;
1182 name_str = args[0].data.str.area;
1183 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001184
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001185 if (args[1].type == ARGT_SINT)
1186 occ = args[1].data.sint;
1187 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001188
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001189 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001190
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001191 idx = &smp->strm->txn->hdr_idx;
1192 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 +02001193
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001194 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1195 /* search for header from the beginning */
1196 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001197
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001198 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1199 /* no explicit occurrence and single fetch => last header by default */
1200 occ = -1;
1201
1202 if (!occ)
1203 /* prepare to report multiple occurrences for ACL fetches */
1204 smp->flags |= SMP_F_NOT_LAST;
1205
1206 smp->data.type = SMP_T_STR;
1207 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1208 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1209 return 1;
1210 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001211 smp->flags &= ~SMP_F_NOT_LAST;
1212 return 0;
1213}
1214
1215/* 6. Check on HTTP header count. The number of occurrences is returned.
1216 * Accepts exactly 1 argument of type string. It does not stop on commas and
1217 * returns full lines instead (useful for User-Agent or Date for example).
1218 */
1219static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1220{
Willy Tarreau79e57332018-10-02 16:01:16 +02001221 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001222
1223 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1224 /* HTX version */
1225 struct htx *htx = smp_prefetch_htx(smp, args);
1226 struct http_hdr_ctx ctx;
1227 struct ist name;
1228
1229 if (!htx)
1230 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001231
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001232 if (args && args->type == ARGT_STR) {
1233 name.ptr = args->data.str.area;
1234 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001235 } else {
1236 name.ptr = NULL;
1237 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001238 }
1239
1240 ctx.blk = NULL;
1241 cnt = 0;
1242 while (http_find_header(htx, name, &ctx, 1))
1243 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001244 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001245 else {
1246 /* LEGACY version */
1247 struct hdr_idx *idx;
1248 struct hdr_ctx ctx;
1249 const struct http_msg *msg;
1250 const char *name = NULL;
1251 int len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001252
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001253 if (args && args->type == ARGT_STR) {
1254 name = args->data.str.area;
1255 len = args->data.str.data;
1256 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001257
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001258 CHECK_HTTP_MESSAGE_FIRST();
1259
1260 idx = &smp->strm->txn->hdr_idx;
1261 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 +02001262
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001263 ctx.idx = 0;
1264 cnt = 0;
1265 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
1266 cnt++;
1267 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001268
1269 smp->data.type = SMP_T_SINT;
1270 smp->data.u.sint = cnt;
1271 smp->flags = SMP_F_VOL_HDR;
1272 return 1;
1273}
1274
1275static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
1276{
Willy Tarreau79e57332018-10-02 16:01:16 +02001277 struct buffer *temp;
1278 char del = ',';
1279
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001280 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1281 /* HTX version */
1282 struct htx *htx = smp_prefetch_htx(smp, args);
1283 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001284
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001285 if (!htx)
1286 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001287
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001288 if (args && args->type == ARGT_STR)
1289 del = *args[0].data.str.area;
Willy Tarreau79e57332018-10-02 16:01:16 +02001290
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001291 temp = get_trash_chunk();
1292 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1293 struct htx_blk *blk = htx_get_blk(htx, pos);
1294 enum htx_blk_type type = htx_get_blk_type(blk);
1295 struct ist n;
Willy Tarreau79e57332018-10-02 16:01:16 +02001296
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001297 if (type == HTX_BLK_EOH)
1298 break;
1299 if (type != HTX_BLK_HDR)
1300 continue;
1301 n = htx_get_blk_name(htx, blk);
1302
1303 if (temp->data)
1304 temp->area[temp->data++] = del;
1305 chunk_memcat(temp, n.ptr, n.len);
1306 }
1307 }
1308 else {
1309 /* LEGACY version */
1310 struct hdr_idx *idx;
1311 struct hdr_ctx ctx;
1312 const struct http_msg *msg;
1313
1314 if (args && args->type == ARGT_STR)
1315 del = *args[0].data.str.area;
1316
1317 CHECK_HTTP_MESSAGE_FIRST();
1318
1319 idx = &smp->strm->txn->hdr_idx;
1320 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
1321
1322 temp = get_trash_chunk();
1323
1324 ctx.idx = 0;
1325 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
1326 if (temp->data)
1327 temp->area[temp->data++] = del;
1328 memcpy(temp->area + temp->data, ctx.line, ctx.del);
1329 temp->data += ctx.del;
1330 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001331 }
1332
1333 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001334 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001335 smp->flags = SMP_F_VOL_HDR;
1336 return 1;
1337}
1338
1339/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1340 * Accepts an optional argument of type string containing the header field name,
1341 * and an optional argument of type signed or unsigned integer to request an
1342 * explicit occurrence of the header. Note that in the event of a missing name,
1343 * headers are considered from the first one.
1344 */
1345static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1346{
Willy Tarreau79e57332018-10-02 16:01:16 +02001347 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001348
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001349 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1350 /* HTX version */
1351 struct htx *htx = smp_prefetch_htx(smp, args);
1352 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1353 struct ist name;
1354
1355 if (!ctx) {
1356 /* first call */
1357 ctx = &static_http_hdr_ctx;
1358 ctx->blk = NULL;
1359 smp->ctx.a[0] = ctx;
1360 }
1361
1362 if (args) {
1363 if (args[0].type != ARGT_STR)
1364 return 0;
1365 name.ptr = args[0].data.str.area;
1366 name.len = args[0].data.str.data;
1367
1368 if (args[1].type == ARGT_SINT)
1369 occ = args[1].data.sint;
1370 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001371
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001372 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001373 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001374
1375 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1376 /* search for header from the beginning */
1377 ctx->blk = NULL;
1378
1379 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1380 /* no explicit occurrence and single fetch => last header by default */
1381 occ = -1;
1382
1383 if (!occ)
1384 /* prepare to report multiple occurrences for ACL fetches */
1385 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001386
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001387 smp->data.type = SMP_T_STR;
1388 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1389 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1390 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001391 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001392 else {
1393 /* LEGACY version */
1394 struct hdr_idx *idx;
1395 struct hdr_ctx *ctx = smp->ctx.a[0];
1396 const struct http_msg *msg;
1397 const char *name_str = NULL;
1398 int name_len = 0;
1399
1400 if (!ctx) {
1401 /* first call */
1402 ctx = &static_hdr_ctx;
1403 ctx->idx = 0;
1404 smp->ctx.a[0] = ctx;
1405 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001406
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001407 if (args) {
1408 if (args[0].type != ARGT_STR)
1409 return 0;
1410 name_str = args[0].data.str.area;
1411 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001412
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001413 if (args[1].type == ARGT_SINT)
1414 occ = args[1].data.sint;
1415 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001416
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001417 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001418
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001419 idx = &smp->strm->txn->hdr_idx;
1420 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 +02001421
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001422 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1423 /* search for header from the beginning */
1424 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001425
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001426 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1427 /* no explicit occurrence and single fetch => last header by default */
1428 occ = -1;
1429
1430 if (!occ)
1431 /* prepare to report multiple occurrences for ACL fetches */
1432 smp->flags |= SMP_F_NOT_LAST;
1433
1434 smp->data.type = SMP_T_STR;
1435 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1436 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1437 return 1;
1438 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001439
1440 smp->flags &= ~SMP_F_NOT_LAST;
1441 return 0;
1442}
1443
1444/* 6. Check on HTTP header count. The number of occurrences is returned.
1445 * Accepts exactly 1 argument of type string.
1446 */
1447static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1448{
Willy Tarreau79e57332018-10-02 16:01:16 +02001449 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001450
1451 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1452 /* HTX version */
1453 struct htx *htx = smp_prefetch_htx(smp, args);
1454 struct http_hdr_ctx ctx;
1455 struct ist name;
1456
1457 if (!htx)
1458 return 0;
1459
1460 if (args && args->type == ARGT_STR) {
1461 name.ptr = args->data.str.area;
1462 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001463 } else {
1464 name.ptr = NULL;
1465 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001466 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001467
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001468 ctx.blk = NULL;
1469 cnt = 0;
1470 while (http_find_header(htx, name, &ctx, 0))
1471 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001472 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001473 else {
1474 /* LEGACY version */
1475 struct hdr_idx *idx;
1476 struct hdr_ctx ctx;
1477 const struct http_msg *msg;
1478 const char *name = NULL;
1479 int len = 0;
1480
1481 if (args && args->type == ARGT_STR) {
1482 name = args->data.str.area;
1483 len = args->data.str.data;
1484 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001485
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001486 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001487
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001488 idx = &smp->strm->txn->hdr_idx;
1489 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 +02001490
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001491 ctx.idx = 0;
1492 cnt = 0;
1493 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
1494 cnt++;
1495 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001496
1497 smp->data.type = SMP_T_SINT;
1498 smp->data.u.sint = cnt;
1499 smp->flags = SMP_F_VOL_HDR;
1500 return 1;
1501}
1502
1503/* Fetch an HTTP header's integer value. The integer value is returned. It
1504 * takes a mandatory argument of type string and an optional one of type int
1505 * to designate a specific occurrence. It returns an unsigned integer, which
1506 * may or may not be appropriate for everything.
1507 */
1508static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1509{
1510 int ret = smp_fetch_hdr(args, smp, kw, private);
1511
1512 if (ret > 0) {
1513 smp->data.type = SMP_T_SINT;
1514 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1515 smp->data.u.str.data);
1516 }
1517
1518 return ret;
1519}
1520
1521/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1522 * and an optional one of type int to designate a specific occurrence.
1523 * It returns an IPv4 or IPv6 address.
1524 */
1525static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1526{
1527 int ret;
1528
1529 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
1530 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
1531 smp->data.type = SMP_T_IPV4;
1532 break;
1533 } else {
1534 struct buffer *temp = get_trash_chunk();
1535 if (smp->data.u.str.data < temp->size - 1) {
1536 memcpy(temp->area, smp->data.u.str.area,
1537 smp->data.u.str.data);
1538 temp->area[smp->data.u.str.data] = '\0';
1539 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1540 smp->data.type = SMP_T_IPV6;
1541 break;
1542 }
1543 }
1544 }
1545
1546 /* if the header doesn't match an IP address, fetch next one */
1547 if (!(smp->flags & SMP_F_NOT_LAST))
1548 return 0;
1549 }
1550 return ret;
1551}
1552
1553/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1554 * the first '/' after the possible hostname, and ends before the possible '?'.
1555 */
1556static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1557{
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001558 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1559 /* HTX version */
1560 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001561 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001562 struct ist path;
1563 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001564
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001565 if (!htx)
1566 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001567
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001568 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001569 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001570 if (!path.ptr)
1571 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001572
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001573 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Willy Tarreau79e57332018-10-02 16:01:16 +02001574
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001575 /* OK, we got the '/' ! */
1576 smp->data.type = SMP_T_STR;
1577 smp->data.u.str.area = path.ptr;
1578 smp->data.u.str.data = len;
1579 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1580 }
1581 else {
1582 struct http_txn *txn;
1583 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001584
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001585 CHECK_HTTP_MESSAGE_FIRST();
1586
1587 txn = smp->strm->txn;
1588 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1589 ptr = http_txn_get_path(txn);
1590 if (!ptr)
1591 return 0;
1592
1593 /* OK, we got the '/' ! */
1594 smp->data.type = SMP_T_STR;
1595 smp->data.u.str.area = ptr;
1596
1597 while (ptr < end && *ptr != '?')
1598 ptr++;
1599
1600 smp->data.u.str.data = ptr - smp->data.u.str.area;
1601 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1602 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001603 return 1;
1604}
1605
1606/* This produces a concatenation of the first occurrence of the Host header
1607 * followed by the path component if it begins with a slash ('/'). This means
1608 * that '*' will not be added, resulting in exactly the first Host entry.
1609 * If no Host header is found, then the path is returned as-is. The returned
1610 * value is stored in the trash so it does not need to be marked constant.
1611 * The returned sample is of type string.
1612 */
1613static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1614{
Willy Tarreau79e57332018-10-02 16:01:16 +02001615 struct buffer *temp;
1616
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001617 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1618 /* HTX version */
1619 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001620 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001621 struct http_hdr_ctx ctx;
1622 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001623
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001624 if (!htx)
1625 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001626
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001627 ctx.blk = NULL;
1628 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1629 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001630
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001631 /* OK we have the header value in ctx.value */
1632 temp = get_trash_chunk();
1633 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1634
1635 /* now retrieve the path */
1636 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001637 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001638 if (path.ptr) {
1639 size_t len;
1640
1641 for (len = 0; len < path.len && *(path.ptr) != '?'; len++);
1642 if (len && *(path.ptr) == '/')
1643 chunk_memcat(temp, path.ptr, len);
1644 }
1645
1646 smp->data.type = SMP_T_STR;
1647 smp->data.u.str = *temp;
1648 }
1649 else {
1650 /* LEGACY version */
1651 struct http_txn *txn;
1652 char *ptr, *end, *beg;
1653 struct hdr_ctx ctx;
1654
1655 CHECK_HTTP_MESSAGE_FIRST();
1656
1657 txn = smp->strm->txn;
1658 ctx.idx = 0;
1659 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
1660 return smp_fetch_path(args, smp, kw, private);
1661
1662 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1663 temp = get_trash_chunk();
1664 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1665 smp->data.type = SMP_T_STR;
1666 smp->data.u.str.area = temp->area;
1667 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001668
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001669 /* now retrieve the path */
1670 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1671 beg = http_txn_get_path(txn);
1672 if (!beg)
1673 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001674
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001675 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1676
1677 if (beg < ptr && *beg == '/') {
1678 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1679 ptr - beg);
1680 smp->data.u.str.data += ptr - beg;
1681 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001682 }
1683
1684 smp->flags = SMP_F_VOL_1ST;
1685 return 1;
1686}
1687
1688/* This produces a 32-bit hash of the concatenation of the first occurrence of
1689 * the Host header followed by the path component if it begins with a slash ('/').
1690 * This means that '*' will not be added, resulting in exactly the first Host
1691 * entry. If no Host header is found, then the path is used. The resulting value
1692 * is hashed using the path hash followed by a full avalanche hash and provides a
1693 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1694 * high-traffic sites without having to store whole paths.
1695 */
1696static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1697{
Willy Tarreau79e57332018-10-02 16:01:16 +02001698 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001699
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001700 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1701 /* HTX version */
1702 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001703 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001704 struct http_hdr_ctx ctx;
1705 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001706
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001707 if (!htx)
1708 return 0;
1709
1710 ctx.blk = NULL;
1711 if (!http_find_header(htx, ist("Host"), &ctx, 0)) {
1712 /* OK we have the header value in ctx.value */
1713 while (ctx.value.len--)
1714 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1715 }
1716
1717 /* now retrieve the path */
1718 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001719 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001720 if (path.ptr) {
1721 size_t len;
1722
1723 for (len = 0; len < path.len && *(path.ptr) != '?'; len++);
1724 if (len && *(path.ptr) == '/') {
1725 while (len--)
1726 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1727 }
1728 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001729 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001730 else {
1731 /* LEGACY version */
1732 struct http_txn *txn;
1733 struct hdr_ctx ctx;
1734 char *ptr, *beg, *end;
1735 int len;
1736
1737 CHECK_HTTP_MESSAGE_FIRST();
1738
1739 txn = smp->strm->txn;
1740 ctx.idx = 0;
1741 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
1742 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1743 ptr = ctx.line + ctx.val;
1744 len = ctx.vlen;
1745 while (len--)
1746 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1747 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001748
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001749 /* now retrieve the path */
1750 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1751 beg = http_txn_get_path(txn);
1752 if (!beg)
1753 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001754
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001755 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001756
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001757 if (beg < ptr && *beg == '/') {
1758 while (beg < ptr)
1759 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
1760 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001761 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001762
Willy Tarreau79e57332018-10-02 16:01:16 +02001763 hash = full_hash(hash);
1764
1765 smp->data.type = SMP_T_SINT;
1766 smp->data.u.sint = hash;
1767 smp->flags = SMP_F_VOL_1ST;
1768 return 1;
1769}
1770
1771/* This concatenates the source address with the 32-bit hash of the Host and
1772 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1773 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1774 * on the source address length. The path hash is stored before the address so
1775 * that in environments where IPv6 is insignificant, truncating the output to
1776 * 8 bytes would still work.
1777 */
1778static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1779{
1780 struct buffer *temp;
1781 struct connection *cli_conn = objt_conn(smp->sess->origin);
1782
1783 if (!cli_conn)
1784 return 0;
1785
1786 if (!smp_fetch_base32(args, smp, kw, private))
1787 return 0;
1788
1789 temp = get_trash_chunk();
1790 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1791 temp->data += sizeof(unsigned int);
1792
1793 switch (cli_conn->addr.from.ss_family) {
1794 case AF_INET:
1795 memcpy(temp->area + temp->data,
1796 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1797 4);
1798 temp->data += 4;
1799 break;
1800 case AF_INET6:
1801 memcpy(temp->area + temp->data,
1802 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1803 16);
1804 temp->data += 16;
1805 break;
1806 default:
1807 return 0;
1808 }
1809
1810 smp->data.u.str = *temp;
1811 smp->data.type = SMP_T_BIN;
1812 return 1;
1813}
1814
1815/* Extracts the query string, which comes after the question mark '?'. If no
1816 * question mark is found, nothing is returned. Otherwise it returns a sample
1817 * of type string carrying the whole query string.
1818 */
1819static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1820{
Willy Tarreau79e57332018-10-02 16:01:16 +02001821 char *ptr, *end;
1822
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001823 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1824 /* HTX version */
1825 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001826 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001827
1828 if (!htx)
1829 return 0;
1830
1831 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001832 ptr = HTX_SL_REQ_UPTR(sl);
1833 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001834 }
1835 else {
1836 /* LEGACY version */
1837 struct http_txn *txn;
1838
1839 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001840
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001841 txn = smp->strm->txn;
1842 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.u;
1843 end = ptr + txn->req.sl.rq.u_l;
1844 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001845
1846 /* look up the '?' */
1847 do {
1848 if (ptr == end)
1849 return 0;
1850 } while (*ptr++ != '?');
1851
1852 smp->data.type = SMP_T_STR;
1853 smp->data.u.str.area = ptr;
1854 smp->data.u.str.data = end - ptr;
1855 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1856 return 1;
1857}
1858
1859static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1860{
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001861 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1862 /* HTX version */
1863 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +02001864
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001865 if (!htx)
1866 return 0;
1867 }
1868 else {
1869 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02001870
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001871 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
1872 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
1873 */
1874 CHECK_HTTP_MESSAGE_FIRST_PERM();
1875 }
1876 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001877 smp->data.u.sint = 1;
1878 return 1;
1879}
1880
1881/* return a valid test if the current request is the first one on the connection */
1882static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1883{
1884 smp->data.type = SMP_T_BOOL;
1885 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1886 return 1;
1887}
1888
1889/* Accepts exactly 1 argument of type userlist */
1890static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1891{
1892
1893 if (!args || args->type != ARGT_USR)
1894 return 0;
1895
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001896 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1897 /* HTX version */
1898 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +02001899
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001900 if (!htx)
1901 return 0;
1902 }
1903 else {
1904 /* LEGACY version */
1905 CHECK_HTTP_MESSAGE_FIRST();
1906 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001907
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001908 if (!get_http_auth(smp))
1909 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001910 smp->data.type = SMP_T_BOOL;
1911 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001912 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001913 return 1;
1914}
1915
1916/* Accepts exactly 1 argument of type userlist */
1917static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1918{
1919 if (!args || args->type != ARGT_USR)
1920 return 0;
1921
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001922 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1923 /* HTX version */
1924 struct htx *htx = smp_prefetch_htx(smp, args);
1925
1926 if (!htx)
1927 return 0;
1928 }
1929 else {
1930 /* LEGACY version */
1931 CHECK_HTTP_MESSAGE_FIRST();
1932 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001933
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001934 if (!get_http_auth(smp))
Willy Tarreau79e57332018-10-02 16:01:16 +02001935 return 0;
1936
1937 /* if the user does not belong to the userlist or has a wrong password,
1938 * report that it unconditionally does not match. Otherwise we return
1939 * a string containing the username.
1940 */
1941 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1942 smp->strm->txn->auth.pass))
1943 return 0;
1944
1945 /* pat_match_auth() will need the user list */
1946 smp->ctx.a[0] = args->data.usr;
1947
1948 smp->data.type = SMP_T_STR;
1949 smp->flags = SMP_F_CONST;
1950 smp->data.u.str.area = smp->strm->txn->auth.user;
1951 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1952
1953 return 1;
1954}
1955
1956/* Fetch a captured HTTP request header. The index is the position of
1957 * the "capture" option in the configuration file
1958 */
1959static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1960{
1961 struct proxy *fe = strm_fe(smp->strm);
1962 int idx;
1963
1964 if (!args || args->type != ARGT_SINT)
1965 return 0;
1966
1967 idx = args->data.sint;
1968
1969 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1970 return 0;
1971
1972 smp->data.type = SMP_T_STR;
1973 smp->flags |= SMP_F_CONST;
1974 smp->data.u.str.area = smp->strm->req_cap[idx];
1975 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1976
1977 return 1;
1978}
1979
1980/* Fetch a captured HTTP response header. The index is the position of
1981 * the "capture" option in the configuration file
1982 */
1983static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1984{
1985 struct proxy *fe = strm_fe(smp->strm);
1986 int idx;
1987
1988 if (!args || args->type != ARGT_SINT)
1989 return 0;
1990
1991 idx = args->data.sint;
1992
1993 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1994 return 0;
1995
1996 smp->data.type = SMP_T_STR;
1997 smp->flags |= SMP_F_CONST;
1998 smp->data.u.str.area = smp->strm->res_cap[idx];
1999 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
2000
2001 return 1;
2002}
2003
2004/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
2005static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
2006{
2007 struct buffer *temp;
2008 struct http_txn *txn = smp->strm->txn;
2009 char *ptr;
2010
2011 if (!txn || !txn->uri)
2012 return 0;
2013
2014 ptr = txn->uri;
2015
2016 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2017 ptr++;
2018
2019 temp = get_trash_chunk();
2020 temp->area = txn->uri;
2021 temp->data = ptr - txn->uri;
2022 smp->data.u.str = *temp;
2023 smp->data.type = SMP_T_STR;
2024 smp->flags = SMP_F_CONST;
2025
2026 return 1;
2027
2028}
2029
2030/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
2031static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
2032{
2033 struct http_txn *txn = smp->strm->txn;
2034 struct ist path;
2035 const char *ptr;
2036
2037 if (!txn || !txn->uri)
2038 return 0;
2039
2040 ptr = txn->uri;
2041
2042 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2043 ptr++;
2044
2045 if (!*ptr)
2046 return 0;
2047
Christopher Faulet78337bb2018-11-15 14:35:18 +01002048 /* skip the first space and find space after URI */
2049 path = ist2(++ptr, 0);
2050 while (*ptr != ' ' && *ptr != '\0')
2051 ptr++;
2052 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002053
Christopher Faulet78337bb2018-11-15 14:35:18 +01002054 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02002055 if (!path.ptr)
2056 return 0;
2057
2058 smp->data.u.str.area = path.ptr;
2059 smp->data.u.str.data = path.len;
2060 smp->data.type = SMP_T_STR;
2061 smp->flags = SMP_F_CONST;
2062
2063 return 1;
2064}
2065
2066/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
2067 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2068 */
2069static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2070{
2071 struct http_txn *txn = smp->strm->txn;
2072
2073 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2074 return 0;
2075
2076 if (txn->req.flags & HTTP_MSGF_VER_11)
2077 smp->data.u.str.area = "HTTP/1.1";
2078 else
2079 smp->data.u.str.area = "HTTP/1.0";
2080
2081 smp->data.u.str.data = 8;
2082 smp->data.type = SMP_T_STR;
2083 smp->flags = SMP_F_CONST;
2084 return 1;
2085
2086}
2087
2088/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2089 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2090 */
2091static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2092{
2093 struct http_txn *txn = smp->strm->txn;
2094
2095 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2096 return 0;
2097
2098 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2099 smp->data.u.str.area = "HTTP/1.1";
2100 else
2101 smp->data.u.str.area = "HTTP/1.0";
2102
2103 smp->data.u.str.data = 8;
2104 smp->data.type = SMP_T_STR;
2105 smp->flags = SMP_F_CONST;
2106 return 1;
2107
2108}
2109
2110/* Iterate over all cookies present in a message. The context is stored in
2111 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2112 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2113 * the direction, multiple cookies may be parsed on the same line or not.
2114 * The cookie name is in args and the name length in args->data.str.len.
2115 * Accepts exactly 1 argument of type string. If the input options indicate
2116 * that no iterating is desired, then only last value is fetched if any.
2117 * The returned sample is of type CSTR. Can be used to parse cookies in other
2118 * files.
2119 */
2120static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2121{
Willy Tarreau79e57332018-10-02 16:01:16 +02002122 int occ = 0;
2123 int found = 0;
2124
2125 if (!args || args->type != ARGT_STR)
2126 return 0;
2127
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002128 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2129 /* HTX version */
2130 struct htx *htx = smp_prefetch_htx(smp, args);
2131 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2132 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002133
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002134 if (!ctx) {
2135 /* first call */
2136 ctx = &static_http_hdr_ctx;
2137 ctx->blk = NULL;
2138 smp->ctx.a[2] = ctx;
2139 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002140
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002141 if (!htx)
2142 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002143
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002144 hdr = (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2145 ? ist("Cookie")
2146 : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002147
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002148 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2149 /* no explicit occurrence and single fetch => last cookie by default */
2150 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02002151
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002152 /* OK so basically here, either we want only one value and it's the
2153 * last one, or we want to iterate over all of them and we fetch the
2154 * next one.
Willy Tarreau79e57332018-10-02 16:01:16 +02002155 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002156
2157 if (!(smp->flags & SMP_F_NOT_LAST)) {
2158 /* search for the header from the beginning, we must first initialize
2159 * the search parameters.
2160 */
2161 smp->ctx.a[0] = NULL;
2162 ctx->blk = NULL;
2163 }
2164
2165 smp->flags |= SMP_F_VOL_HDR;
2166 while (1) {
2167 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2168 if (!smp->ctx.a[0]) {
2169 if (!http_find_header(htx, hdr, ctx, 0))
2170 goto out;
2171
2172 if (ctx->value.len < args->data.str.data + 1)
2173 continue;
2174
2175 smp->ctx.a[0] = ctx->value.ptr;
2176 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2177 }
2178
2179 smp->data.type = SMP_T_STR;
2180 smp->flags |= SMP_F_CONST;
2181 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2182 args->data.str.area, args->data.str.data,
2183 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2184 &smp->data.u.str.area,
2185 &smp->data.u.str.data);
2186 if (smp->ctx.a[0]) {
2187 found = 1;
2188 if (occ >= 0) {
2189 /* one value was returned into smp->data.u.str.{str,len} */
2190 smp->flags |= SMP_F_NOT_LAST;
2191 return 1;
2192 }
2193 }
2194 /* if we're looking for last occurrence, let's loop */
2195 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002196 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002197 else {
2198 /* LEGACY version */
2199 struct http_txn *txn;
2200 struct hdr_idx *idx;
2201 struct hdr_ctx *ctx = smp->ctx.a[2];
2202 const struct http_msg *msg;
2203 const char *hdr_name;
2204 int hdr_name_len;
2205 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002206
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002207 if (!ctx) {
2208 /* first call */
2209 ctx = &static_hdr_ctx;
2210 ctx->idx = 0;
2211 smp->ctx.a[2] = ctx;
2212 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002213
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002214 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02002215
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002216 txn = smp->strm->txn;
2217 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +02002218
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002219 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
2220 msg = &txn->req;
2221 hdr_name = "Cookie";
2222 hdr_name_len = 6;
2223 } else {
2224 msg = &txn->rsp;
2225 hdr_name = "Set-Cookie";
2226 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002227 }
2228
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002229 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2230 /* no explicit occurrence and single fetch => last cookie by default */
2231 occ = -1;
2232
2233 /* OK so basically here, either we want only one value and it's the
2234 * last one, or we want to iterate over all of them and we fetch the
2235 * next one.
2236 */
2237
2238 sol = ci_head(msg->chn);
2239 if (!(smp->flags & SMP_F_NOT_LAST)) {
2240 /* search for the header from the beginning, we must first initialize
2241 * the search parameters.
2242 */
2243 smp->ctx.a[0] = NULL;
2244 ctx->idx = 0;
2245 }
2246
2247 smp->flags |= SMP_F_VOL_HDR;
2248
2249 while (1) {
2250 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2251 if (!smp->ctx.a[0]) {
2252 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2253 goto out;
2254
2255 if (ctx->vlen < args->data.str.data + 1)
2256 continue;
2257
2258 smp->ctx.a[0] = ctx->line + ctx->val;
2259 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2260 }
2261
2262 smp->data.type = SMP_T_STR;
2263 smp->flags |= SMP_F_CONST;
2264 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
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, &smp->data.u.str.data);
2268 if (smp->ctx.a[0]) {
2269 found = 1;
2270 if (occ >= 0) {
2271 /* one value was returned into smp->data.u.str.{str,len} */
2272 smp->flags |= SMP_F_NOT_LAST;
2273 return 1;
2274 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002275 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002276 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002277 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002278 }
2279 /* all cookie headers and values were scanned. If we're looking for the
2280 * last occurrence, we may return it now.
2281 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002282 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002283 smp->flags &= ~SMP_F_NOT_LAST;
2284 return found;
2285}
2286
2287/* Iterate over all cookies present in a request to count how many occurrences
2288 * match the name in args and args->data.str.len. If <multi> is non-null, then
2289 * multiple cookies may be parsed on the same line. The returned sample is of
2290 * type UINT. Accepts exactly 1 argument of type string.
2291 */
2292static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2293{
Willy Tarreau79e57332018-10-02 16:01:16 +02002294 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002295 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002296
2297 if (!args || args->type != ARGT_STR)
2298 return 0;
2299
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002300 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2301 /* HTX version */
2302 struct htx *htx = smp_prefetch_htx(smp, args);
2303 struct http_hdr_ctx ctx;
2304 struct ist hdr;
2305
2306 if (!htx)
2307 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002308
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002309 hdr = (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2310 ? ist("Cookie")
2311 : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002312
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002313 val_end = val_beg = NULL;
2314 ctx.blk = NULL;
2315 cnt = 0;
2316 while (1) {
2317 /* Note: val_beg == NULL every time we need to fetch a new header */
2318 if (!val_beg) {
2319 if (!http_find_header(htx, hdr, &ctx, 0))
2320 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002321
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002322 if (ctx.value.len < args->data.str.data + 1)
2323 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002324
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002325 val_beg = ctx.value.ptr;
2326 val_end = val_beg + ctx.value.len;
2327 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002328
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002329 smp->data.type = SMP_T_STR;
2330 smp->flags |= SMP_F_CONST;
2331 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2332 args->data.str.area, args->data.str.data,
2333 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2334 &smp->data.u.str.area,
2335 &smp->data.u.str.data))) {
2336 cnt++;
2337 }
2338 }
2339 }
2340 else {
2341 /* LEGACY version */
2342 struct http_txn *txn;
2343 struct hdr_idx *idx;
2344 struct hdr_ctx ctx;
2345 const struct http_msg *msg;
2346 const char *hdr_name;
2347 int hdr_name_len;
2348 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002349
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002350 CHECK_HTTP_MESSAGE_FIRST();
2351
2352 txn = smp->strm->txn;
2353 idx = &smp->strm->txn->hdr_idx;
2354
2355 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
2356 msg = &txn->req;
2357 hdr_name = "Cookie";
2358 hdr_name_len = 6;
2359 } else {
2360 msg = &txn->rsp;
2361 hdr_name = "Set-Cookie";
2362 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002363 }
2364
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002365 sol = ci_head(msg->chn);
2366 val_end = val_beg = NULL;
2367 ctx.idx = 0;
2368 cnt = 0;
2369
2370 while (1) {
2371 /* Note: val_beg == NULL every time we need to fetch a new header */
2372 if (!val_beg) {
2373 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2374 break;
2375
2376 if (ctx.vlen < args->data.str.data + 1)
2377 continue;
2378
2379 val_beg = ctx.line + ctx.val;
2380 val_end = val_beg + ctx.vlen;
2381 }
2382
2383 smp->data.type = SMP_T_STR;
2384 smp->flags |= SMP_F_CONST;
2385 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2386 args->data.str.area, args->data.str.data,
2387 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2388 &smp->data.u.str.area, &smp->data.u.str.data))) {
2389 cnt++;
2390 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002391 }
2392 }
2393
2394 smp->data.type = SMP_T_SINT;
2395 smp->data.u.sint = cnt;
2396 smp->flags |= SMP_F_VOL_HDR;
2397 return 1;
2398}
2399
2400/* Fetch an cookie's integer value. The integer value is returned. It
2401 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2402 */
2403static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2404{
2405 int ret = smp_fetch_cookie(args, smp, kw, private);
2406
2407 if (ret > 0) {
2408 smp->data.type = SMP_T_SINT;
2409 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2410 smp->data.u.str.data);
2411 }
2412
2413 return ret;
2414}
2415
2416/************************************************************************/
2417/* The code below is dedicated to sample fetches */
2418/************************************************************************/
2419
2420/* This scans a URL-encoded query string. It takes an optionally wrapping
2421 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2422 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2423 * pointers are updated for next iteration before leaving.
2424 */
2425static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2426{
2427 const char *vstart, *vend;
2428 struct buffer *temp;
2429 const char **chunks = (const char **)smp->ctx.a;
2430
2431 if (!http_find_next_url_param(chunks, name, name_len,
2432 &vstart, &vend, delim))
2433 return 0;
2434
2435 /* Create sample. If the value is contiguous, return the pointer as CONST,
2436 * if the value is wrapped, copy-it in a buffer.
2437 */
2438 smp->data.type = SMP_T_STR;
2439 if (chunks[2] &&
2440 vstart >= chunks[0] && vstart <= chunks[1] &&
2441 vend >= chunks[2] && vend <= chunks[3]) {
2442 /* Wrapped case. */
2443 temp = get_trash_chunk();
2444 memcpy(temp->area, vstart, chunks[1] - vstart);
2445 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2446 vend - chunks[2]);
2447 smp->data.u.str.area = temp->area;
2448 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2449 } else {
2450 /* Contiguous case. */
2451 smp->data.u.str.area = (char *)vstart;
2452 smp->data.u.str.data = vend - vstart;
2453 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2454 }
2455
2456 /* Update context, check wrapping. */
2457 chunks[0] = vend;
2458 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2459 chunks[1] = chunks[3];
2460 chunks[2] = NULL;
2461 }
2462
2463 if (chunks[0] < chunks[1])
2464 smp->flags |= SMP_F_NOT_LAST;
2465
2466 return 1;
2467}
2468
2469/* This function iterates over each parameter of the query string. It uses
2470 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2471 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2472 * An optional parameter name is passed in args[0], otherwise any parameter is
2473 * considered. It supports an optional delimiter argument for the beginning of
2474 * the string in args[1], which defaults to "?".
2475 */
2476static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2477{
Willy Tarreau79e57332018-10-02 16:01:16 +02002478 char delim = '?';
2479 const char *name;
2480 int name_len;
2481
2482 if (!args ||
2483 (args[0].type && args[0].type != ARGT_STR) ||
2484 (args[1].type && args[1].type != ARGT_STR))
2485 return 0;
2486
2487 name = "";
2488 name_len = 0;
2489 if (args->type == ARGT_STR) {
2490 name = args->data.str.area;
2491 name_len = args->data.str.data;
2492 }
2493
2494 if (args[1].type)
2495 delim = *args[1].data.str.area;
2496
2497 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002498 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2499 /* HTX version */
2500 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002501 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002502
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002503 if (!htx)
2504 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002505
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002506 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002507 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002508 if (!smp->ctx.a[0])
2509 return 0;
2510
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002511 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002512 }
2513 else {
2514 /* LEGACY version */
2515 struct http_msg *msg;
2516
2517 CHECK_HTTP_MESSAGE_FIRST();
2518
2519 msg = &smp->strm->txn->req;
2520
2521 smp->ctx.a[0] = http_find_param_list(ci_head(msg->chn) + msg->sl.rq.u,
2522 msg->sl.rq.u_l, delim);
2523 if (!smp->ctx.a[0])
2524 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002525
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002526 smp->ctx.a[1] = ci_head(msg->chn) + msg->sl.rq.u + msg->sl.rq.u_l;
2527 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002528
2529 /* Assume that the context is filled with NULL pointer
2530 * before the first call.
2531 * smp->ctx.a[2] = NULL;
2532 * smp->ctx.a[3] = NULL;
2533 */
2534 }
2535
2536 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2537}
2538
2539/* This function iterates over each parameter of the body. This requires
2540 * that the body has been waited for using http-buffer-request. It uses
2541 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2542 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2543 * optional second part if the body wraps at the end of the buffer. An optional
2544 * parameter name is passed in args[0], otherwise any parameter is considered.
2545 */
2546static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2547{
Willy Tarreau79e57332018-10-02 16:01:16 +02002548 const char *name;
2549 int name_len;
2550
2551 if (!args || (args[0].type && args[0].type != ARGT_STR))
2552 return 0;
2553
2554 name = "";
2555 name_len = 0;
2556 if (args[0].type == ARGT_STR) {
2557 name = args[0].data.str.area;
2558 name_len = args[0].data.str.data;
2559 }
2560
2561 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002562 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2563 /* HTX version */
2564 struct htx *htx = smp_prefetch_htx(smp, args);
2565 struct buffer *temp;
2566 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002567
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002568 if (!htx)
2569 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002570
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002571 temp = get_trash_chunk();
2572 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
2573 struct htx_blk *blk = htx_get_blk(htx, pos);
2574 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002575
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002576 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
2577 break;
2578 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01002579 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002580 return 0;
2581 }
2582 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002583
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002584 smp->ctx.a[0] = temp->area;
2585 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002586
2587 /* Assume that the context is filled with NULL pointer
2588 * before the first call.
2589 * smp->ctx.a[2] = NULL;
2590 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002591 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002592 }
2593 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002594 /* LEGACY version */
2595 struct http_msg *msg;
2596 unsigned long len;
2597 unsigned long block1;
2598 char *body;
2599
2600 CHECK_HTTP_MESSAGE_FIRST();
2601
2602 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2603 msg = &smp->strm->txn->req;
2604 else
2605 msg = &smp->strm->txn->rsp;
2606
2607 len = http_body_bytes(msg);
2608 body = c_ptr(msg->chn, -http_data_rewind(msg));
2609
2610 block1 = len;
2611 if (block1 > b_wrap(&msg->chn->buf) - body)
2612 block1 = b_wrap(&msg->chn->buf) - body;
2613
2614 if (block1 == len) {
2615 /* buffer is not wrapped (or empty) */
2616 smp->ctx.a[0] = body;
2617 smp->ctx.a[1] = body + len;
2618
2619 /* Assume that the context is filled with NULL pointer
2620 * before the first call.
2621 * smp->ctx.a[2] = NULL;
2622 * smp->ctx.a[3] = NULL;
2623 */
2624 }
2625 else {
2626 /* buffer is wrapped, we need to defragment it */
2627 smp->ctx.a[0] = body;
2628 smp->ctx.a[1] = body + block1;
2629 smp->ctx.a[2] = b_orig(&msg->chn->buf);
2630 smp->ctx.a[3] = b_orig(&msg->chn->buf) + ( len - block1 );
2631 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002632 }
2633 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002634
Willy Tarreau79e57332018-10-02 16:01:16 +02002635 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2636}
2637
2638/* Return the signed integer value for the specified url parameter (see url_param
2639 * above).
2640 */
2641static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2642{
2643 int ret = smp_fetch_url_param(args, smp, kw, private);
2644
2645 if (ret > 0) {
2646 smp->data.type = SMP_T_SINT;
2647 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2648 smp->data.u.str.data);
2649 }
2650
2651 return ret;
2652}
2653
2654/* This produces a 32-bit hash of the concatenation of the first occurrence of
2655 * the Host header followed by the path component if it begins with a slash ('/').
2656 * This means that '*' will not be added, resulting in exactly the first Host
2657 * entry. If no Host header is found, then the path is used. The resulting value
2658 * is hashed using the url hash followed by a full avalanche hash and provides a
2659 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2660 * high-traffic sites without having to store whole paths.
2661 * this differs from the base32 functions in that it includes the url parameters
2662 * as well as the path
2663 */
2664static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2665{
Willy Tarreau79e57332018-10-02 16:01:16 +02002666 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002667
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002668 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2669 /* HTX version */
2670 struct htx *htx = smp_prefetch_htx(smp, args);
2671 struct http_hdr_ctx ctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002672 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002673 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002674
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002675 if (!htx)
2676 return 0;
2677
2678 ctx.blk = NULL;
2679 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2680 /* OK we have the header value in ctx.value */
2681 while (ctx.value.len--)
2682 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2683 }
2684
2685 /* now retrieve the path */
2686 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002687 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002688 while (path.len > 0 && *(path.ptr) != '?') {
2689 path.ptr++;
2690 path.len--;
2691 }
2692 if (path.len && *(path.ptr) == '/') {
2693 while (path.len--)
2694 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2695 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002696 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002697 else {
2698 /* LEGACY version */
2699 struct http_txn *txn;
2700 struct hdr_ctx ctx;
2701 char *ptr, *beg, *end;
2702 int len;
2703
2704 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02002705
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002706 txn = smp->strm->txn;
2707 ctx.idx = 0;
2708 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
2709 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2710 ptr = ctx.line + ctx.val;
2711 len = ctx.vlen;
2712 while (len--)
2713 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2714 }
2715
2716 /* now retrieve the path */
2717 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
2718 beg = http_txn_get_path(txn);
2719 if (!beg)
2720 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002721
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002722 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002723
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002724 if (beg < ptr && *beg == '/') {
2725 while (beg < ptr)
2726 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2727 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002728 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002729
Willy Tarreau79e57332018-10-02 16:01:16 +02002730 hash = full_hash(hash);
2731
2732 smp->data.type = SMP_T_SINT;
2733 smp->data.u.sint = hash;
2734 smp->flags = SMP_F_VOL_1ST;
2735 return 1;
2736}
2737
2738/* This concatenates the source address with the 32-bit hash of the Host and
2739 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2740 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2741 * on the source address length. The URL hash is stored before the address so
2742 * that in environments where IPv6 is insignificant, truncating the output to
2743 * 8 bytes would still work.
2744 */
2745static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2746{
2747 struct buffer *temp;
2748 struct connection *cli_conn = objt_conn(smp->sess->origin);
2749
2750 if (!cli_conn)
2751 return 0;
2752
2753 if (!smp_fetch_url32(args, smp, kw, private))
2754 return 0;
2755
2756 temp = get_trash_chunk();
2757 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2758 temp->data += sizeof(unsigned int);
2759
2760 switch (cli_conn->addr.from.ss_family) {
2761 case AF_INET:
2762 memcpy(temp->area + temp->data,
2763 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2764 4);
2765 temp->data += 4;
2766 break;
2767 case AF_INET6:
2768 memcpy(temp->area + temp->data,
2769 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2770 16);
2771 temp->data += 16;
2772 break;
2773 default:
2774 return 0;
2775 }
2776
2777 smp->data.u.str = *temp;
2778 smp->data.type = SMP_T_BIN;
2779 return 1;
2780}
2781
2782/************************************************************************/
2783/* Other utility functions */
2784/************************************************************************/
2785
2786/* This function is used to validate the arguments passed to any "hdr" fetch
2787 * keyword. These keywords support an optional positive or negative occurrence
2788 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2789 * is assumed that the types are already the correct ones. Returns 0 on error,
2790 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2791 * error message in case of error, that the caller is responsible for freeing.
2792 * The initial location must either be freeable or NULL.
2793 * Note: this function's pointer is checked from Lua.
2794 */
2795int val_hdr(struct arg *arg, char **err_msg)
2796{
2797 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2798 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2799 return 0;
2800 }
2801 return 1;
2802}
2803
2804/************************************************************************/
2805/* All supported sample fetch keywords must be declared here. */
2806/************************************************************************/
2807
2808/* Note: must not be declared <const> as its list will be overwritten */
2809static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2810 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2811 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2812 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2813
2814 /* capture are allocated and are permanent in the stream */
2815 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2816
2817 /* retrieve these captures from the HTTP logs */
2818 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2819 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2820 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2821
2822 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2823 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2824
2825 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2826 * are only here to match the ACL's name, are request-only and are used
2827 * for ACL compatibility only.
2828 */
2829 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2830 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
2831 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2832 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2833
2834 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2835 * only here to match the ACL's name, are request-only and are used for
2836 * ACL compatibility only.
2837 */
2838 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
2839 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2840 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2841 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2842
2843 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2844 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2845 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2846 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2847 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2848 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2849
2850 /* HTTP protocol on the request path */
2851 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2852 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2853
2854 /* HTTP version on the request path */
2855 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2856 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2857
2858 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2859 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2860 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2861 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2862
2863 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2864 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2865
2866 /* HTTP version on the response path */
2867 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2868 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2869
2870 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2871 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2872 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2873 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2874
2875 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2876 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2877 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2878 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2879 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2880 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2881 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2882
2883 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2884 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2885 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2886 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2887
2888 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2889 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2890 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2891 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2892 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2893 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2894 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2895
2896 /* scook is valid only on the response and is used for ACL compatibility */
2897 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2898 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2899 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2900 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2901
2902 /* shdr is valid only on the response and is used for ACL compatibility */
2903 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2904 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2905 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2906 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2907
2908 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2909 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2910 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2911 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2912 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2913 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2914 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2915 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2916 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2917 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2918 { /* END */ },
2919}};
2920
Willy Tarreau0108d902018-11-25 19:14:37 +01002921INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002922
2923/*
2924 * Local variables:
2925 * c-indent-level: 8
2926 * c-basic-offset: 8
2927 * End:
2928 */