blob: 68c455cb8b019b22afed8e256fd0c1f2ad43cfd3 [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 */
920 return 0; /* TODO: to be implemented */
921 }
922 else {
923 /* LEGACY version */
924 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200925
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200926 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +0200927
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200928 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
929 msg = &smp->strm->txn->req;
930 else
931 msg = &smp->strm->txn->rsp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200932
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200933 smp->data.type = SMP_T_SINT;
934 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreau79e57332018-10-02 16:01:16 +0200935
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200936 smp->flags = SMP_F_VOL_TEST;
937 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200938 return 1;
939}
940
941
942/* returns the advertised length of the body, or the advertised size of the
943 * chunks available in the buffer. This requires that the body has been waited
944 * for using http-buffer-request.
945 */
946static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
947{
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200948 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
949 /* HTX version */
950 return 0; /* TODO: to be implemented */
951 }
952 else {
953 /* LEGACY version */
954 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200955
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200956 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +0200957
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200958 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
959 msg = &smp->strm->txn->req;
960 else
961 msg = &smp->strm->txn->rsp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200962
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200963 smp->data.type = SMP_T_SINT;
964 smp->data.u.sint = msg->body_len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200965
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200966 smp->flags = SMP_F_VOL_TEST;
967 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200968 return 1;
969}
970
971
972/* 4. Check on URL/URI. A pointer to the URI is stored. */
973static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
974{
975 struct http_txn *txn;
976
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200977 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
978 /* HTX version */
979 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100980 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200981
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200982 if (!htx)
983 return 0;
984 sl = http_find_stline(htx);
985 smp->data.type = SMP_T_STR;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100986 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
987 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200988 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
989 }
990 else {
991 /* LEGACY version */
992 CHECK_HTTP_MESSAGE_FIRST();
993
994 txn = smp->strm->txn;
995 smp->data.type = SMP_T_STR;
996 smp->data.u.str.data = txn->req.sl.rq.u_l;
997 smp->data.u.str.area = ci_head(txn->req.chn) + txn->req.sl.rq.u;
998 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
999 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001000 return 1;
1001}
1002
1003static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1004{
1005 struct http_txn *txn;
1006 struct sockaddr_storage addr;
1007
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001008 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1009 /* HTX version */
1010 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001011 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001012
1013 if (!htx)
1014 return 0;
1015 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001016 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001017 }
1018 else {
1019 /* LEGACY version */
1020 CHECK_HTTP_MESSAGE_FIRST();
1021
1022 txn = smp->strm->txn;
1023 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
1024 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001025
Willy Tarreau79e57332018-10-02 16:01:16 +02001026 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1027 return 0;
1028
1029 smp->data.type = SMP_T_IPV4;
1030 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
1031 smp->flags = 0;
1032 return 1;
1033}
1034
1035static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
1036{
1037 struct http_txn *txn;
1038 struct sockaddr_storage addr;
1039
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001040 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1041 /* HTX version */
1042 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001043 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001044
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001045 if (!htx)
1046 return 0;
1047 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001048 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001049 }
1050 else {
1051 /* LEGACY version */
1052 CHECK_HTTP_MESSAGE_FIRST();
1053
1054 txn = smp->strm->txn;
1055 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
1056 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001057 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1058 return 0;
1059
1060 smp->data.type = SMP_T_SINT;
1061 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
1062 smp->flags = 0;
1063 return 1;
1064}
1065
1066/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1067 * Accepts an optional argument of type string containing the header field name,
1068 * and an optional argument of type signed or unsigned integer to request an
1069 * explicit occurrence of the header. Note that in the event of a missing name,
1070 * headers are considered from the first one. It does not stop on commas and
1071 * returns full lines instead (useful for User-Agent or Date for example).
1072 */
1073static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1074{
Willy Tarreau79e57332018-10-02 16:01:16 +02001075 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001076
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001077 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1078 /* HTX version */
1079 struct htx *htx = smp_prefetch_htx(smp, args);
1080 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1081 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +02001082
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001083 if (!ctx) {
1084 /* first call */
1085 ctx = &static_http_hdr_ctx;
1086 ctx->blk = NULL;
1087 smp->ctx.a[0] = ctx;
1088 }
1089
1090 if (args) {
1091 if (args[0].type != ARGT_STR)
1092 return 0;
1093 name.ptr = args[0].data.str.area;
1094 name.len = args[0].data.str.data;
1095
1096 if (args[1].type == ARGT_SINT)
1097 occ = args[1].data.sint;
1098 }
1099
1100 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001101 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001102
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001103 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1104 /* search for header from the beginning */
1105 ctx->blk = NULL;
1106
1107 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1108 /* no explicit occurrence and single fetch => last header by default */
1109 occ = -1;
1110
1111 if (!occ)
1112 /* prepare to report multiple occurrences for ACL fetches */
1113 smp->flags |= SMP_F_NOT_LAST;
1114
1115 smp->data.type = SMP_T_STR;
1116 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1117 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1118 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001119 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001120 else {
1121 /* LEGACY version */
1122 struct hdr_idx *idx;
1123 struct hdr_ctx *ctx = smp->ctx.a[0];
1124 const struct http_msg *msg;
1125 const char *name_str = NULL;
1126 int name_len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001127
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001128 if (!ctx) {
1129 /* first call */
1130 ctx = &static_hdr_ctx;
1131 ctx->idx = 0;
1132 smp->ctx.a[0] = ctx;
1133 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001134
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001135 if (args) {
1136 if (args[0].type != ARGT_STR)
1137 return 0;
1138 name_str = args[0].data.str.area;
1139 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001140
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001141 if (args[1].type == ARGT_SINT)
1142 occ = args[1].data.sint;
1143 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001144
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001145 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001146
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001147 idx = &smp->strm->txn->hdr_idx;
1148 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 +02001149
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001150 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1151 /* search for header from the beginning */
1152 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001153
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001154 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1155 /* no explicit occurrence and single fetch => last header by default */
1156 occ = -1;
1157
1158 if (!occ)
1159 /* prepare to report multiple occurrences for ACL fetches */
1160 smp->flags |= SMP_F_NOT_LAST;
1161
1162 smp->data.type = SMP_T_STR;
1163 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1164 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1165 return 1;
1166 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001167 smp->flags &= ~SMP_F_NOT_LAST;
1168 return 0;
1169}
1170
1171/* 6. Check on HTTP header count. The number of occurrences is returned.
1172 * Accepts exactly 1 argument of type string. It does not stop on commas and
1173 * returns full lines instead (useful for User-Agent or Date for example).
1174 */
1175static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1176{
Willy Tarreau79e57332018-10-02 16:01:16 +02001177 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001178
1179 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1180 /* HTX version */
1181 struct htx *htx = smp_prefetch_htx(smp, args);
1182 struct http_hdr_ctx ctx;
1183 struct ist name;
1184
1185 if (!htx)
1186 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001187
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001188 if (args && args->type == ARGT_STR) {
1189 name.ptr = args->data.str.area;
1190 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001191 } else {
1192 name.ptr = NULL;
1193 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001194 }
1195
1196 ctx.blk = NULL;
1197 cnt = 0;
1198 while (http_find_header(htx, name, &ctx, 1))
1199 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001200 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001201 else {
1202 /* LEGACY version */
1203 struct hdr_idx *idx;
1204 struct hdr_ctx ctx;
1205 const struct http_msg *msg;
1206 const char *name = NULL;
1207 int len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001208
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001209 if (args && args->type == ARGT_STR) {
1210 name = args->data.str.area;
1211 len = args->data.str.data;
1212 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001213
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001214 CHECK_HTTP_MESSAGE_FIRST();
1215
1216 idx = &smp->strm->txn->hdr_idx;
1217 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 +02001218
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001219 ctx.idx = 0;
1220 cnt = 0;
1221 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
1222 cnt++;
1223 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001224
1225 smp->data.type = SMP_T_SINT;
1226 smp->data.u.sint = cnt;
1227 smp->flags = SMP_F_VOL_HDR;
1228 return 1;
1229}
1230
1231static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
1232{
Willy Tarreau79e57332018-10-02 16:01:16 +02001233 struct buffer *temp;
1234 char del = ',';
1235
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001236 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1237 /* HTX version */
1238 struct htx *htx = smp_prefetch_htx(smp, args);
1239 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001240
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001241 if (!htx)
1242 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001243
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001244 if (args && args->type == ARGT_STR)
1245 del = *args[0].data.str.area;
Willy Tarreau79e57332018-10-02 16:01:16 +02001246
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001247 temp = get_trash_chunk();
1248 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1249 struct htx_blk *blk = htx_get_blk(htx, pos);
1250 enum htx_blk_type type = htx_get_blk_type(blk);
1251 struct ist n;
Willy Tarreau79e57332018-10-02 16:01:16 +02001252
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001253 if (type == HTX_BLK_EOH)
1254 break;
1255 if (type != HTX_BLK_HDR)
1256 continue;
1257 n = htx_get_blk_name(htx, blk);
1258
1259 if (temp->data)
1260 temp->area[temp->data++] = del;
1261 chunk_memcat(temp, n.ptr, n.len);
1262 }
1263 }
1264 else {
1265 /* LEGACY version */
1266 struct hdr_idx *idx;
1267 struct hdr_ctx ctx;
1268 const struct http_msg *msg;
1269
1270 if (args && args->type == ARGT_STR)
1271 del = *args[0].data.str.area;
1272
1273 CHECK_HTTP_MESSAGE_FIRST();
1274
1275 idx = &smp->strm->txn->hdr_idx;
1276 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
1277
1278 temp = get_trash_chunk();
1279
1280 ctx.idx = 0;
1281 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
1282 if (temp->data)
1283 temp->area[temp->data++] = del;
1284 memcpy(temp->area + temp->data, ctx.line, ctx.del);
1285 temp->data += ctx.del;
1286 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001287 }
1288
1289 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001290 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001291 smp->flags = SMP_F_VOL_HDR;
1292 return 1;
1293}
1294
1295/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1296 * Accepts an optional argument of type string containing the header field name,
1297 * and an optional argument of type signed or unsigned integer to request an
1298 * explicit occurrence of the header. Note that in the event of a missing name,
1299 * headers are considered from the first one.
1300 */
1301static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1302{
Willy Tarreau79e57332018-10-02 16:01:16 +02001303 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001304
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001305 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1306 /* HTX version */
1307 struct htx *htx = smp_prefetch_htx(smp, args);
1308 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1309 struct ist name;
1310
1311 if (!ctx) {
1312 /* first call */
1313 ctx = &static_http_hdr_ctx;
1314 ctx->blk = NULL;
1315 smp->ctx.a[0] = ctx;
1316 }
1317
1318 if (args) {
1319 if (args[0].type != ARGT_STR)
1320 return 0;
1321 name.ptr = args[0].data.str.area;
1322 name.len = args[0].data.str.data;
1323
1324 if (args[1].type == ARGT_SINT)
1325 occ = args[1].data.sint;
1326 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001327
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001328 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001329 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001330
1331 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1332 /* search for header from the beginning */
1333 ctx->blk = NULL;
1334
1335 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1336 /* no explicit occurrence and single fetch => last header by default */
1337 occ = -1;
1338
1339 if (!occ)
1340 /* prepare to report multiple occurrences for ACL fetches */
1341 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001342
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001343 smp->data.type = SMP_T_STR;
1344 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1345 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1346 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001347 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001348 else {
1349 /* LEGACY version */
1350 struct hdr_idx *idx;
1351 struct hdr_ctx *ctx = smp->ctx.a[0];
1352 const struct http_msg *msg;
1353 const char *name_str = NULL;
1354 int name_len = 0;
1355
1356 if (!ctx) {
1357 /* first call */
1358 ctx = &static_hdr_ctx;
1359 ctx->idx = 0;
1360 smp->ctx.a[0] = ctx;
1361 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001362
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001363 if (args) {
1364 if (args[0].type != ARGT_STR)
1365 return 0;
1366 name_str = args[0].data.str.area;
1367 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001368
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001369 if (args[1].type == ARGT_SINT)
1370 occ = args[1].data.sint;
1371 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001372
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001373 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001374
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001375 idx = &smp->strm->txn->hdr_idx;
1376 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 +02001377
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001378 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1379 /* search for header from the beginning */
1380 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001381
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001382 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1383 /* no explicit occurrence and single fetch => last header by default */
1384 occ = -1;
1385
1386 if (!occ)
1387 /* prepare to report multiple occurrences for ACL fetches */
1388 smp->flags |= SMP_F_NOT_LAST;
1389
1390 smp->data.type = SMP_T_STR;
1391 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1392 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1393 return 1;
1394 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001395
1396 smp->flags &= ~SMP_F_NOT_LAST;
1397 return 0;
1398}
1399
1400/* 6. Check on HTTP header count. The number of occurrences is returned.
1401 * Accepts exactly 1 argument of type string.
1402 */
1403static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1404{
Willy Tarreau79e57332018-10-02 16:01:16 +02001405 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001406
1407 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1408 /* HTX version */
1409 struct htx *htx = smp_prefetch_htx(smp, args);
1410 struct http_hdr_ctx ctx;
1411 struct ist name;
1412
1413 if (!htx)
1414 return 0;
1415
1416 if (args && args->type == ARGT_STR) {
1417 name.ptr = args->data.str.area;
1418 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001419 } else {
1420 name.ptr = NULL;
1421 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001422 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001423
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001424 ctx.blk = NULL;
1425 cnt = 0;
1426 while (http_find_header(htx, name, &ctx, 0))
1427 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001428 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001429 else {
1430 /* LEGACY version */
1431 struct hdr_idx *idx;
1432 struct hdr_ctx ctx;
1433 const struct http_msg *msg;
1434 const char *name = NULL;
1435 int len = 0;
1436
1437 if (args && args->type == ARGT_STR) {
1438 name = args->data.str.area;
1439 len = args->data.str.data;
1440 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001441
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001442 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001443
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001444 idx = &smp->strm->txn->hdr_idx;
1445 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 +02001446
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001447 ctx.idx = 0;
1448 cnt = 0;
1449 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
1450 cnt++;
1451 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001452
1453 smp->data.type = SMP_T_SINT;
1454 smp->data.u.sint = cnt;
1455 smp->flags = SMP_F_VOL_HDR;
1456 return 1;
1457}
1458
1459/* Fetch an HTTP header's integer value. The integer value is returned. It
1460 * takes a mandatory argument of type string and an optional one of type int
1461 * to designate a specific occurrence. It returns an unsigned integer, which
1462 * may or may not be appropriate for everything.
1463 */
1464static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1465{
1466 int ret = smp_fetch_hdr(args, smp, kw, private);
1467
1468 if (ret > 0) {
1469 smp->data.type = SMP_T_SINT;
1470 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1471 smp->data.u.str.data);
1472 }
1473
1474 return ret;
1475}
1476
1477/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1478 * and an optional one of type int to designate a specific occurrence.
1479 * It returns an IPv4 or IPv6 address.
1480 */
1481static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1482{
1483 int ret;
1484
1485 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
1486 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
1487 smp->data.type = SMP_T_IPV4;
1488 break;
1489 } else {
1490 struct buffer *temp = get_trash_chunk();
1491 if (smp->data.u.str.data < temp->size - 1) {
1492 memcpy(temp->area, smp->data.u.str.area,
1493 smp->data.u.str.data);
1494 temp->area[smp->data.u.str.data] = '\0';
1495 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1496 smp->data.type = SMP_T_IPV6;
1497 break;
1498 }
1499 }
1500 }
1501
1502 /* if the header doesn't match an IP address, fetch next one */
1503 if (!(smp->flags & SMP_F_NOT_LAST))
1504 return 0;
1505 }
1506 return ret;
1507}
1508
1509/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1510 * the first '/' after the possible hostname, and ends before the possible '?'.
1511 */
1512static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1513{
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001514 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1515 /* HTX version */
1516 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001517 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001518 struct ist path;
1519 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001520
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001521 if (!htx)
1522 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001523
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001524 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001525 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001526 if (!path.ptr)
1527 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001528
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001529 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Willy Tarreau79e57332018-10-02 16:01:16 +02001530
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001531 /* OK, we got the '/' ! */
1532 smp->data.type = SMP_T_STR;
1533 smp->data.u.str.area = path.ptr;
1534 smp->data.u.str.data = len;
1535 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1536 }
1537 else {
1538 struct http_txn *txn;
1539 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001540
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001541 CHECK_HTTP_MESSAGE_FIRST();
1542
1543 txn = smp->strm->txn;
1544 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1545 ptr = http_txn_get_path(txn);
1546 if (!ptr)
1547 return 0;
1548
1549 /* OK, we got the '/' ! */
1550 smp->data.type = SMP_T_STR;
1551 smp->data.u.str.area = ptr;
1552
1553 while (ptr < end && *ptr != '?')
1554 ptr++;
1555
1556 smp->data.u.str.data = ptr - smp->data.u.str.area;
1557 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1558 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001559 return 1;
1560}
1561
1562/* This produces a concatenation of the first occurrence of the Host header
1563 * followed by the path component if it begins with a slash ('/'). This means
1564 * that '*' will not be added, resulting in exactly the first Host entry.
1565 * If no Host header is found, then the path is returned as-is. The returned
1566 * value is stored in the trash so it does not need to be marked constant.
1567 * The returned sample is of type string.
1568 */
1569static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1570{
Willy Tarreau79e57332018-10-02 16:01:16 +02001571 struct buffer *temp;
1572
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001573 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1574 /* HTX version */
1575 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001576 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001577 struct http_hdr_ctx ctx;
1578 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001579
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001580 if (!htx)
1581 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001582
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001583 ctx.blk = NULL;
1584 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1585 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001586
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001587 /* OK we have the header value in ctx.value */
1588 temp = get_trash_chunk();
1589 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1590
1591 /* now retrieve the path */
1592 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001593 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001594 if (path.ptr) {
1595 size_t len;
1596
1597 for (len = 0; len < path.len && *(path.ptr) != '?'; len++);
1598 if (len && *(path.ptr) == '/')
1599 chunk_memcat(temp, path.ptr, len);
1600 }
1601
1602 smp->data.type = SMP_T_STR;
1603 smp->data.u.str = *temp;
1604 }
1605 else {
1606 /* LEGACY version */
1607 struct http_txn *txn;
1608 char *ptr, *end, *beg;
1609 struct hdr_ctx ctx;
1610
1611 CHECK_HTTP_MESSAGE_FIRST();
1612
1613 txn = smp->strm->txn;
1614 ctx.idx = 0;
1615 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
1616 return smp_fetch_path(args, smp, kw, private);
1617
1618 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1619 temp = get_trash_chunk();
1620 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1621 smp->data.type = SMP_T_STR;
1622 smp->data.u.str.area = temp->area;
1623 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001624
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001625 /* now retrieve the path */
1626 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1627 beg = http_txn_get_path(txn);
1628 if (!beg)
1629 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001630
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001631 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1632
1633 if (beg < ptr && *beg == '/') {
1634 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1635 ptr - beg);
1636 smp->data.u.str.data += ptr - beg;
1637 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001638 }
1639
1640 smp->flags = SMP_F_VOL_1ST;
1641 return 1;
1642}
1643
1644/* This produces a 32-bit hash of the concatenation of the first occurrence of
1645 * the Host header followed by the path component if it begins with a slash ('/').
1646 * This means that '*' will not be added, resulting in exactly the first Host
1647 * entry. If no Host header is found, then the path is used. The resulting value
1648 * is hashed using the path hash followed by a full avalanche hash and provides a
1649 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1650 * high-traffic sites without having to store whole paths.
1651 */
1652static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1653{
Willy Tarreau79e57332018-10-02 16:01:16 +02001654 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001655
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001656 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1657 /* HTX version */
1658 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001659 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001660 struct http_hdr_ctx ctx;
1661 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001662
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001663 if (!htx)
1664 return 0;
1665
1666 ctx.blk = NULL;
1667 if (!http_find_header(htx, ist("Host"), &ctx, 0)) {
1668 /* OK we have the header value in ctx.value */
1669 while (ctx.value.len--)
1670 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1671 }
1672
1673 /* now retrieve the path */
1674 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001675 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001676 if (path.ptr) {
1677 size_t len;
1678
1679 for (len = 0; len < path.len && *(path.ptr) != '?'; len++);
1680 if (len && *(path.ptr) == '/') {
1681 while (len--)
1682 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1683 }
1684 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001685 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001686 else {
1687 /* LEGACY version */
1688 struct http_txn *txn;
1689 struct hdr_ctx ctx;
1690 char *ptr, *beg, *end;
1691 int len;
1692
1693 CHECK_HTTP_MESSAGE_FIRST();
1694
1695 txn = smp->strm->txn;
1696 ctx.idx = 0;
1697 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
1698 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1699 ptr = ctx.line + ctx.val;
1700 len = ctx.vlen;
1701 while (len--)
1702 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1703 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001704
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001705 /* now retrieve the path */
1706 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1707 beg = http_txn_get_path(txn);
1708 if (!beg)
1709 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001710
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001711 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001712
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001713 if (beg < ptr && *beg == '/') {
1714 while (beg < ptr)
1715 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
1716 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001717 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001718
Willy Tarreau79e57332018-10-02 16:01:16 +02001719 hash = full_hash(hash);
1720
1721 smp->data.type = SMP_T_SINT;
1722 smp->data.u.sint = hash;
1723 smp->flags = SMP_F_VOL_1ST;
1724 return 1;
1725}
1726
1727/* This concatenates the source address with the 32-bit hash of the Host and
1728 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1729 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1730 * on the source address length. The path hash is stored before the address so
1731 * that in environments where IPv6 is insignificant, truncating the output to
1732 * 8 bytes would still work.
1733 */
1734static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1735{
1736 struct buffer *temp;
1737 struct connection *cli_conn = objt_conn(smp->sess->origin);
1738
1739 if (!cli_conn)
1740 return 0;
1741
1742 if (!smp_fetch_base32(args, smp, kw, private))
1743 return 0;
1744
1745 temp = get_trash_chunk();
1746 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1747 temp->data += sizeof(unsigned int);
1748
1749 switch (cli_conn->addr.from.ss_family) {
1750 case AF_INET:
1751 memcpy(temp->area + temp->data,
1752 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1753 4);
1754 temp->data += 4;
1755 break;
1756 case AF_INET6:
1757 memcpy(temp->area + temp->data,
1758 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1759 16);
1760 temp->data += 16;
1761 break;
1762 default:
1763 return 0;
1764 }
1765
1766 smp->data.u.str = *temp;
1767 smp->data.type = SMP_T_BIN;
1768 return 1;
1769}
1770
1771/* Extracts the query string, which comes after the question mark '?'. If no
1772 * question mark is found, nothing is returned. Otherwise it returns a sample
1773 * of type string carrying the whole query string.
1774 */
1775static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1776{
Willy Tarreau79e57332018-10-02 16:01:16 +02001777 char *ptr, *end;
1778
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001779 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1780 /* HTX version */
1781 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001782 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001783
1784 if (!htx)
1785 return 0;
1786
1787 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001788 ptr = HTX_SL_REQ_UPTR(sl);
1789 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001790 }
1791 else {
1792 /* LEGACY version */
1793 struct http_txn *txn;
1794
1795 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001796
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001797 txn = smp->strm->txn;
1798 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.u;
1799 end = ptr + txn->req.sl.rq.u_l;
1800 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001801
1802 /* look up the '?' */
1803 do {
1804 if (ptr == end)
1805 return 0;
1806 } while (*ptr++ != '?');
1807
1808 smp->data.type = SMP_T_STR;
1809 smp->data.u.str.area = ptr;
1810 smp->data.u.str.data = end - ptr;
1811 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1812 return 1;
1813}
1814
1815static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1816{
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001817 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1818 /* HTX version */
1819 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +02001820
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001821 if (!htx)
1822 return 0;
1823 }
1824 else {
1825 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02001826
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001827 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
1828 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
1829 */
1830 CHECK_HTTP_MESSAGE_FIRST_PERM();
1831 }
1832 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001833 smp->data.u.sint = 1;
1834 return 1;
1835}
1836
1837/* return a valid test if the current request is the first one on the connection */
1838static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1839{
1840 smp->data.type = SMP_T_BOOL;
1841 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1842 return 1;
1843}
1844
1845/* Accepts exactly 1 argument of type userlist */
1846static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1847{
1848
1849 if (!args || args->type != ARGT_USR)
1850 return 0;
1851
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001852 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1853 /* HTX version */
1854 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +02001855
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001856 if (!htx)
1857 return 0;
1858 }
1859 else {
1860 /* LEGACY version */
1861 CHECK_HTTP_MESSAGE_FIRST();
1862 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001863
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001864 if (!get_http_auth(smp))
1865 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001866 smp->data.type = SMP_T_BOOL;
1867 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001868 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001869 return 1;
1870}
1871
1872/* Accepts exactly 1 argument of type userlist */
1873static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1874{
1875 if (!args || args->type != ARGT_USR)
1876 return 0;
1877
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001878 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1879 /* HTX version */
1880 struct htx *htx = smp_prefetch_htx(smp, args);
1881
1882 if (!htx)
1883 return 0;
1884 }
1885 else {
1886 /* LEGACY version */
1887 CHECK_HTTP_MESSAGE_FIRST();
1888 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001889
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001890 if (!get_http_auth(smp))
Willy Tarreau79e57332018-10-02 16:01:16 +02001891 return 0;
1892
1893 /* if the user does not belong to the userlist or has a wrong password,
1894 * report that it unconditionally does not match. Otherwise we return
1895 * a string containing the username.
1896 */
1897 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1898 smp->strm->txn->auth.pass))
1899 return 0;
1900
1901 /* pat_match_auth() will need the user list */
1902 smp->ctx.a[0] = args->data.usr;
1903
1904 smp->data.type = SMP_T_STR;
1905 smp->flags = SMP_F_CONST;
1906 smp->data.u.str.area = smp->strm->txn->auth.user;
1907 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1908
1909 return 1;
1910}
1911
1912/* Fetch a captured HTTP request header. The index is the position of
1913 * the "capture" option in the configuration file
1914 */
1915static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1916{
1917 struct proxy *fe = strm_fe(smp->strm);
1918 int idx;
1919
1920 if (!args || args->type != ARGT_SINT)
1921 return 0;
1922
1923 idx = args->data.sint;
1924
1925 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1926 return 0;
1927
1928 smp->data.type = SMP_T_STR;
1929 smp->flags |= SMP_F_CONST;
1930 smp->data.u.str.area = smp->strm->req_cap[idx];
1931 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1932
1933 return 1;
1934}
1935
1936/* Fetch a captured HTTP response header. The index is the position of
1937 * the "capture" option in the configuration file
1938 */
1939static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1940{
1941 struct proxy *fe = strm_fe(smp->strm);
1942 int idx;
1943
1944 if (!args || args->type != ARGT_SINT)
1945 return 0;
1946
1947 idx = args->data.sint;
1948
1949 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1950 return 0;
1951
1952 smp->data.type = SMP_T_STR;
1953 smp->flags |= SMP_F_CONST;
1954 smp->data.u.str.area = smp->strm->res_cap[idx];
1955 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1956
1957 return 1;
1958}
1959
1960/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1961static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1962{
1963 struct buffer *temp;
1964 struct http_txn *txn = smp->strm->txn;
1965 char *ptr;
1966
1967 if (!txn || !txn->uri)
1968 return 0;
1969
1970 ptr = txn->uri;
1971
1972 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1973 ptr++;
1974
1975 temp = get_trash_chunk();
1976 temp->area = txn->uri;
1977 temp->data = ptr - txn->uri;
1978 smp->data.u.str = *temp;
1979 smp->data.type = SMP_T_STR;
1980 smp->flags = SMP_F_CONST;
1981
1982 return 1;
1983
1984}
1985
1986/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1987static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1988{
1989 struct http_txn *txn = smp->strm->txn;
1990 struct ist path;
1991 const char *ptr;
1992
1993 if (!txn || !txn->uri)
1994 return 0;
1995
1996 ptr = txn->uri;
1997
1998 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1999 ptr++;
2000
2001 if (!*ptr)
2002 return 0;
2003
Christopher Faulet78337bb2018-11-15 14:35:18 +01002004 /* skip the first space and find space after URI */
2005 path = ist2(++ptr, 0);
2006 while (*ptr != ' ' && *ptr != '\0')
2007 ptr++;
2008 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002009
Christopher Faulet78337bb2018-11-15 14:35:18 +01002010 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02002011 if (!path.ptr)
2012 return 0;
2013
2014 smp->data.u.str.area = path.ptr;
2015 smp->data.u.str.data = path.len;
2016 smp->data.type = SMP_T_STR;
2017 smp->flags = SMP_F_CONST;
2018
2019 return 1;
2020}
2021
2022/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
2023 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2024 */
2025static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2026{
2027 struct http_txn *txn = smp->strm->txn;
2028
2029 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2030 return 0;
2031
2032 if (txn->req.flags & HTTP_MSGF_VER_11)
2033 smp->data.u.str.area = "HTTP/1.1";
2034 else
2035 smp->data.u.str.area = "HTTP/1.0";
2036
2037 smp->data.u.str.data = 8;
2038 smp->data.type = SMP_T_STR;
2039 smp->flags = SMP_F_CONST;
2040 return 1;
2041
2042}
2043
2044/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2045 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2046 */
2047static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2048{
2049 struct http_txn *txn = smp->strm->txn;
2050
2051 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2052 return 0;
2053
2054 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2055 smp->data.u.str.area = "HTTP/1.1";
2056 else
2057 smp->data.u.str.area = "HTTP/1.0";
2058
2059 smp->data.u.str.data = 8;
2060 smp->data.type = SMP_T_STR;
2061 smp->flags = SMP_F_CONST;
2062 return 1;
2063
2064}
2065
2066/* Iterate over all cookies present in a message. The context is stored in
2067 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2068 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2069 * the direction, multiple cookies may be parsed on the same line or not.
2070 * The cookie name is in args and the name length in args->data.str.len.
2071 * Accepts exactly 1 argument of type string. If the input options indicate
2072 * that no iterating is desired, then only last value is fetched if any.
2073 * The returned sample is of type CSTR. Can be used to parse cookies in other
2074 * files.
2075 */
2076static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2077{
Willy Tarreau79e57332018-10-02 16:01:16 +02002078 int occ = 0;
2079 int found = 0;
2080
2081 if (!args || args->type != ARGT_STR)
2082 return 0;
2083
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002084 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2085 /* HTX version */
2086 struct htx *htx = smp_prefetch_htx(smp, args);
2087 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2088 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002089
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002090 if (!ctx) {
2091 /* first call */
2092 ctx = &static_http_hdr_ctx;
2093 ctx->blk = NULL;
2094 smp->ctx.a[2] = ctx;
2095 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002096
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002097 if (!htx)
2098 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002099
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002100 hdr = (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2101 ? ist("Cookie")
2102 : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002103
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002104 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2105 /* no explicit occurrence and single fetch => last cookie by default */
2106 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02002107
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002108 /* OK so basically here, either we want only one value and it's the
2109 * last one, or we want to iterate over all of them and we fetch the
2110 * next one.
Willy Tarreau79e57332018-10-02 16:01:16 +02002111 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002112
2113 if (!(smp->flags & SMP_F_NOT_LAST)) {
2114 /* search for the header from the beginning, we must first initialize
2115 * the search parameters.
2116 */
2117 smp->ctx.a[0] = NULL;
2118 ctx->blk = NULL;
2119 }
2120
2121 smp->flags |= SMP_F_VOL_HDR;
2122 while (1) {
2123 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2124 if (!smp->ctx.a[0]) {
2125 if (!http_find_header(htx, hdr, ctx, 0))
2126 goto out;
2127
2128 if (ctx->value.len < args->data.str.data + 1)
2129 continue;
2130
2131 smp->ctx.a[0] = ctx->value.ptr;
2132 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2133 }
2134
2135 smp->data.type = SMP_T_STR;
2136 smp->flags |= SMP_F_CONST;
2137 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2138 args->data.str.area, args->data.str.data,
2139 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2140 &smp->data.u.str.area,
2141 &smp->data.u.str.data);
2142 if (smp->ctx.a[0]) {
2143 found = 1;
2144 if (occ >= 0) {
2145 /* one value was returned into smp->data.u.str.{str,len} */
2146 smp->flags |= SMP_F_NOT_LAST;
2147 return 1;
2148 }
2149 }
2150 /* if we're looking for last occurrence, let's loop */
2151 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002152 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002153 else {
2154 /* LEGACY version */
2155 struct http_txn *txn;
2156 struct hdr_idx *idx;
2157 struct hdr_ctx *ctx = smp->ctx.a[2];
2158 const struct http_msg *msg;
2159 const char *hdr_name;
2160 int hdr_name_len;
2161 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002162
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002163 if (!ctx) {
2164 /* first call */
2165 ctx = &static_hdr_ctx;
2166 ctx->idx = 0;
2167 smp->ctx.a[2] = ctx;
2168 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002169
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002170 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02002171
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002172 txn = smp->strm->txn;
2173 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +02002174
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002175 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
2176 msg = &txn->req;
2177 hdr_name = "Cookie";
2178 hdr_name_len = 6;
2179 } else {
2180 msg = &txn->rsp;
2181 hdr_name = "Set-Cookie";
2182 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002183 }
2184
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002185 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2186 /* no explicit occurrence and single fetch => last cookie by default */
2187 occ = -1;
2188
2189 /* OK so basically here, either we want only one value and it's the
2190 * last one, or we want to iterate over all of them and we fetch the
2191 * next one.
2192 */
2193
2194 sol = ci_head(msg->chn);
2195 if (!(smp->flags & SMP_F_NOT_LAST)) {
2196 /* search for the header from the beginning, we must first initialize
2197 * the search parameters.
2198 */
2199 smp->ctx.a[0] = NULL;
2200 ctx->idx = 0;
2201 }
2202
2203 smp->flags |= SMP_F_VOL_HDR;
2204
2205 while (1) {
2206 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2207 if (!smp->ctx.a[0]) {
2208 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2209 goto out;
2210
2211 if (ctx->vlen < args->data.str.data + 1)
2212 continue;
2213
2214 smp->ctx.a[0] = ctx->line + ctx->val;
2215 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2216 }
2217
2218 smp->data.type = SMP_T_STR;
2219 smp->flags |= SMP_F_CONST;
2220 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2221 args->data.str.area, args->data.str.data,
2222 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2223 &smp->data.u.str.area, &smp->data.u.str.data);
2224 if (smp->ctx.a[0]) {
2225 found = 1;
2226 if (occ >= 0) {
2227 /* one value was returned into smp->data.u.str.{str,len} */
2228 smp->flags |= SMP_F_NOT_LAST;
2229 return 1;
2230 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002231 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002232 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002233 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002234 }
2235 /* all cookie headers and values were scanned. If we're looking for the
2236 * last occurrence, we may return it now.
2237 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002238 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002239 smp->flags &= ~SMP_F_NOT_LAST;
2240 return found;
2241}
2242
2243/* Iterate over all cookies present in a request to count how many occurrences
2244 * match the name in args and args->data.str.len. If <multi> is non-null, then
2245 * multiple cookies may be parsed on the same line. The returned sample is of
2246 * type UINT. Accepts exactly 1 argument of type string.
2247 */
2248static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2249{
Willy Tarreau79e57332018-10-02 16:01:16 +02002250 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002251 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002252
2253 if (!args || args->type != ARGT_STR)
2254 return 0;
2255
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002256 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2257 /* HTX version */
2258 struct htx *htx = smp_prefetch_htx(smp, args);
2259 struct http_hdr_ctx ctx;
2260 struct ist hdr;
2261
2262 if (!htx)
2263 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002264
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002265 hdr = (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2266 ? ist("Cookie")
2267 : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002268
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002269 val_end = val_beg = NULL;
2270 ctx.blk = NULL;
2271 cnt = 0;
2272 while (1) {
2273 /* Note: val_beg == NULL every time we need to fetch a new header */
2274 if (!val_beg) {
2275 if (!http_find_header(htx, hdr, &ctx, 0))
2276 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002277
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002278 if (ctx.value.len < args->data.str.data + 1)
2279 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002280
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002281 val_beg = ctx.value.ptr;
2282 val_end = val_beg + ctx.value.len;
2283 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002284
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002285 smp->data.type = SMP_T_STR;
2286 smp->flags |= SMP_F_CONST;
2287 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2288 args->data.str.area, args->data.str.data,
2289 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2290 &smp->data.u.str.area,
2291 &smp->data.u.str.data))) {
2292 cnt++;
2293 }
2294 }
2295 }
2296 else {
2297 /* LEGACY version */
2298 struct http_txn *txn;
2299 struct hdr_idx *idx;
2300 struct hdr_ctx ctx;
2301 const struct http_msg *msg;
2302 const char *hdr_name;
2303 int hdr_name_len;
2304 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002305
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002306 CHECK_HTTP_MESSAGE_FIRST();
2307
2308 txn = smp->strm->txn;
2309 idx = &smp->strm->txn->hdr_idx;
2310
2311 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
2312 msg = &txn->req;
2313 hdr_name = "Cookie";
2314 hdr_name_len = 6;
2315 } else {
2316 msg = &txn->rsp;
2317 hdr_name = "Set-Cookie";
2318 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002319 }
2320
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002321 sol = ci_head(msg->chn);
2322 val_end = val_beg = NULL;
2323 ctx.idx = 0;
2324 cnt = 0;
2325
2326 while (1) {
2327 /* Note: val_beg == NULL every time we need to fetch a new header */
2328 if (!val_beg) {
2329 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2330 break;
2331
2332 if (ctx.vlen < args->data.str.data + 1)
2333 continue;
2334
2335 val_beg = ctx.line + ctx.val;
2336 val_end = val_beg + ctx.vlen;
2337 }
2338
2339 smp->data.type = SMP_T_STR;
2340 smp->flags |= SMP_F_CONST;
2341 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2342 args->data.str.area, args->data.str.data,
2343 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2344 &smp->data.u.str.area, &smp->data.u.str.data))) {
2345 cnt++;
2346 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002347 }
2348 }
2349
2350 smp->data.type = SMP_T_SINT;
2351 smp->data.u.sint = cnt;
2352 smp->flags |= SMP_F_VOL_HDR;
2353 return 1;
2354}
2355
2356/* Fetch an cookie's integer value. The integer value is returned. It
2357 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2358 */
2359static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2360{
2361 int ret = smp_fetch_cookie(args, smp, kw, private);
2362
2363 if (ret > 0) {
2364 smp->data.type = SMP_T_SINT;
2365 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2366 smp->data.u.str.data);
2367 }
2368
2369 return ret;
2370}
2371
2372/************************************************************************/
2373/* The code below is dedicated to sample fetches */
2374/************************************************************************/
2375
2376/* This scans a URL-encoded query string. It takes an optionally wrapping
2377 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2378 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2379 * pointers are updated for next iteration before leaving.
2380 */
2381static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2382{
2383 const char *vstart, *vend;
2384 struct buffer *temp;
2385 const char **chunks = (const char **)smp->ctx.a;
2386
2387 if (!http_find_next_url_param(chunks, name, name_len,
2388 &vstart, &vend, delim))
2389 return 0;
2390
2391 /* Create sample. If the value is contiguous, return the pointer as CONST,
2392 * if the value is wrapped, copy-it in a buffer.
2393 */
2394 smp->data.type = SMP_T_STR;
2395 if (chunks[2] &&
2396 vstart >= chunks[0] && vstart <= chunks[1] &&
2397 vend >= chunks[2] && vend <= chunks[3]) {
2398 /* Wrapped case. */
2399 temp = get_trash_chunk();
2400 memcpy(temp->area, vstart, chunks[1] - vstart);
2401 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2402 vend - chunks[2]);
2403 smp->data.u.str.area = temp->area;
2404 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2405 } else {
2406 /* Contiguous case. */
2407 smp->data.u.str.area = (char *)vstart;
2408 smp->data.u.str.data = vend - vstart;
2409 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2410 }
2411
2412 /* Update context, check wrapping. */
2413 chunks[0] = vend;
2414 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2415 chunks[1] = chunks[3];
2416 chunks[2] = NULL;
2417 }
2418
2419 if (chunks[0] < chunks[1])
2420 smp->flags |= SMP_F_NOT_LAST;
2421
2422 return 1;
2423}
2424
2425/* This function iterates over each parameter of the query string. It uses
2426 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2427 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2428 * An optional parameter name is passed in args[0], otherwise any parameter is
2429 * considered. It supports an optional delimiter argument for the beginning of
2430 * the string in args[1], which defaults to "?".
2431 */
2432static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2433{
Willy Tarreau79e57332018-10-02 16:01:16 +02002434 char delim = '?';
2435 const char *name;
2436 int name_len;
2437
2438 if (!args ||
2439 (args[0].type && args[0].type != ARGT_STR) ||
2440 (args[1].type && args[1].type != ARGT_STR))
2441 return 0;
2442
2443 name = "";
2444 name_len = 0;
2445 if (args->type == ARGT_STR) {
2446 name = args->data.str.area;
2447 name_len = args->data.str.data;
2448 }
2449
2450 if (args[1].type)
2451 delim = *args[1].data.str.area;
2452
2453 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002454 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2455 /* HTX version */
2456 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002457 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002458
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002459 if (!htx)
2460 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002461
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002462 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002463 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 +02002464 if (!smp->ctx.a[0])
2465 return 0;
2466
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002467 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002468 }
2469 else {
2470 /* LEGACY version */
2471 struct http_msg *msg;
2472
2473 CHECK_HTTP_MESSAGE_FIRST();
2474
2475 msg = &smp->strm->txn->req;
2476
2477 smp->ctx.a[0] = http_find_param_list(ci_head(msg->chn) + msg->sl.rq.u,
2478 msg->sl.rq.u_l, delim);
2479 if (!smp->ctx.a[0])
2480 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002481
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002482 smp->ctx.a[1] = ci_head(msg->chn) + msg->sl.rq.u + msg->sl.rq.u_l;
2483 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002484
2485 /* Assume that the context is filled with NULL pointer
2486 * before the first call.
2487 * smp->ctx.a[2] = NULL;
2488 * smp->ctx.a[3] = NULL;
2489 */
2490 }
2491
2492 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2493}
2494
2495/* This function iterates over each parameter of the body. This requires
2496 * that the body has been waited for using http-buffer-request. It uses
2497 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2498 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2499 * optional second part if the body wraps at the end of the buffer. An optional
2500 * parameter name is passed in args[0], otherwise any parameter is considered.
2501 */
2502static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2503{
Willy Tarreau79e57332018-10-02 16:01:16 +02002504 const char *name;
2505 int name_len;
2506
2507 if (!args || (args[0].type && args[0].type != ARGT_STR))
2508 return 0;
2509
2510 name = "";
2511 name_len = 0;
2512 if (args[0].type == ARGT_STR) {
2513 name = args[0].data.str.area;
2514 name_len = args[0].data.str.data;
2515 }
2516
2517 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002518 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2519 /* HTX version */
2520 struct htx *htx = smp_prefetch_htx(smp, args);
2521 struct buffer *temp;
2522 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002523
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002524 if (!htx)
2525 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002526
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002527 temp = get_trash_chunk();
2528 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
2529 struct htx_blk *blk = htx_get_blk(htx, pos);
2530 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002531
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002532 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
2533 break;
2534 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01002535 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002536 return 0;
2537 }
2538 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002539
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002540 smp->ctx.a[0] = temp->area;
2541 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002542
2543 /* Assume that the context is filled with NULL pointer
2544 * before the first call.
2545 * smp->ctx.a[2] = NULL;
2546 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002547 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002548 }
2549 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002550 /* LEGACY version */
2551 struct http_msg *msg;
2552 unsigned long len;
2553 unsigned long block1;
2554 char *body;
2555
2556 CHECK_HTTP_MESSAGE_FIRST();
2557
2558 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2559 msg = &smp->strm->txn->req;
2560 else
2561 msg = &smp->strm->txn->rsp;
2562
2563 len = http_body_bytes(msg);
2564 body = c_ptr(msg->chn, -http_data_rewind(msg));
2565
2566 block1 = len;
2567 if (block1 > b_wrap(&msg->chn->buf) - body)
2568 block1 = b_wrap(&msg->chn->buf) - body;
2569
2570 if (block1 == len) {
2571 /* buffer is not wrapped (or empty) */
2572 smp->ctx.a[0] = body;
2573 smp->ctx.a[1] = body + len;
2574
2575 /* Assume that the context is filled with NULL pointer
2576 * before the first call.
2577 * smp->ctx.a[2] = NULL;
2578 * smp->ctx.a[3] = NULL;
2579 */
2580 }
2581 else {
2582 /* buffer is wrapped, we need to defragment it */
2583 smp->ctx.a[0] = body;
2584 smp->ctx.a[1] = body + block1;
2585 smp->ctx.a[2] = b_orig(&msg->chn->buf);
2586 smp->ctx.a[3] = b_orig(&msg->chn->buf) + ( len - block1 );
2587 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002588 }
2589 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002590
Willy Tarreau79e57332018-10-02 16:01:16 +02002591 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2592}
2593
2594/* Return the signed integer value for the specified url parameter (see url_param
2595 * above).
2596 */
2597static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2598{
2599 int ret = smp_fetch_url_param(args, smp, kw, private);
2600
2601 if (ret > 0) {
2602 smp->data.type = SMP_T_SINT;
2603 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2604 smp->data.u.str.data);
2605 }
2606
2607 return ret;
2608}
2609
2610/* This produces a 32-bit hash of the concatenation of the first occurrence of
2611 * the Host header followed by the path component if it begins with a slash ('/').
2612 * This means that '*' will not be added, resulting in exactly the first Host
2613 * entry. If no Host header is found, then the path is used. The resulting value
2614 * is hashed using the url hash followed by a full avalanche hash and provides a
2615 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2616 * high-traffic sites without having to store whole paths.
2617 * this differs from the base32 functions in that it includes the url parameters
2618 * as well as the path
2619 */
2620static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2621{
Willy Tarreau79e57332018-10-02 16:01:16 +02002622 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002623
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002624 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2625 /* HTX version */
2626 struct htx *htx = smp_prefetch_htx(smp, args);
2627 struct http_hdr_ctx ctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002628 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002629 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002630
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002631 if (!htx)
2632 return 0;
2633
2634 ctx.blk = NULL;
2635 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2636 /* OK we have the header value in ctx.value */
2637 while (ctx.value.len--)
2638 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2639 }
2640
2641 /* now retrieve the path */
2642 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002643 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002644 while (path.len > 0 && *(path.ptr) != '?') {
2645 path.ptr++;
2646 path.len--;
2647 }
2648 if (path.len && *(path.ptr) == '/') {
2649 while (path.len--)
2650 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2651 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002652 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002653 else {
2654 /* LEGACY version */
2655 struct http_txn *txn;
2656 struct hdr_ctx ctx;
2657 char *ptr, *beg, *end;
2658 int len;
2659
2660 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02002661
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002662 txn = smp->strm->txn;
2663 ctx.idx = 0;
2664 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
2665 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2666 ptr = ctx.line + ctx.val;
2667 len = ctx.vlen;
2668 while (len--)
2669 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2670 }
2671
2672 /* now retrieve the path */
2673 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
2674 beg = http_txn_get_path(txn);
2675 if (!beg)
2676 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002677
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002678 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002679
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002680 if (beg < ptr && *beg == '/') {
2681 while (beg < ptr)
2682 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2683 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002684 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002685
Willy Tarreau79e57332018-10-02 16:01:16 +02002686 hash = full_hash(hash);
2687
2688 smp->data.type = SMP_T_SINT;
2689 smp->data.u.sint = hash;
2690 smp->flags = SMP_F_VOL_1ST;
2691 return 1;
2692}
2693
2694/* This concatenates the source address with the 32-bit hash of the Host and
2695 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2696 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2697 * on the source address length. The URL hash is stored before the address so
2698 * that in environments where IPv6 is insignificant, truncating the output to
2699 * 8 bytes would still work.
2700 */
2701static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2702{
2703 struct buffer *temp;
2704 struct connection *cli_conn = objt_conn(smp->sess->origin);
2705
2706 if (!cli_conn)
2707 return 0;
2708
2709 if (!smp_fetch_url32(args, smp, kw, private))
2710 return 0;
2711
2712 temp = get_trash_chunk();
2713 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2714 temp->data += sizeof(unsigned int);
2715
2716 switch (cli_conn->addr.from.ss_family) {
2717 case AF_INET:
2718 memcpy(temp->area + temp->data,
2719 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2720 4);
2721 temp->data += 4;
2722 break;
2723 case AF_INET6:
2724 memcpy(temp->area + temp->data,
2725 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2726 16);
2727 temp->data += 16;
2728 break;
2729 default:
2730 return 0;
2731 }
2732
2733 smp->data.u.str = *temp;
2734 smp->data.type = SMP_T_BIN;
2735 return 1;
2736}
2737
2738/************************************************************************/
2739/* Other utility functions */
2740/************************************************************************/
2741
2742/* This function is used to validate the arguments passed to any "hdr" fetch
2743 * keyword. These keywords support an optional positive or negative occurrence
2744 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2745 * is assumed that the types are already the correct ones. Returns 0 on error,
2746 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2747 * error message in case of error, that the caller is responsible for freeing.
2748 * The initial location must either be freeable or NULL.
2749 * Note: this function's pointer is checked from Lua.
2750 */
2751int val_hdr(struct arg *arg, char **err_msg)
2752{
2753 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2754 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2755 return 0;
2756 }
2757 return 1;
2758}
2759
2760/************************************************************************/
2761/* All supported sample fetch keywords must be declared here. */
2762/************************************************************************/
2763
2764/* Note: must not be declared <const> as its list will be overwritten */
2765static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2766 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2767 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2768 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2769
2770 /* capture are allocated and are permanent in the stream */
2771 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2772
2773 /* retrieve these captures from the HTTP logs */
2774 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2775 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2776 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2777
2778 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2779 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2780
2781 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2782 * are only here to match the ACL's name, are request-only and are used
2783 * for ACL compatibility only.
2784 */
2785 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2786 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
2787 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2788 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2789
2790 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2791 * only here to match the ACL's name, are request-only and are used for
2792 * ACL compatibility only.
2793 */
2794 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
2795 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2796 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2797 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2798
2799 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2800 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2801 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2802 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2803 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2804 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2805
2806 /* HTTP protocol on the request path */
2807 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2808 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2809
2810 /* HTTP version on the request path */
2811 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2812 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2813
2814 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2815 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2816 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2817 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2818
2819 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2820 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2821
2822 /* HTTP version on the response path */
2823 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2824 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2825
2826 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2827 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2828 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2829 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2830
2831 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2832 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2833 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2834 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2835 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2836 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2837 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2838
2839 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2840 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2841 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2842 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2843
2844 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2845 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2846 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2847 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2848 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2849 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2850 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2851
2852 /* scook is valid only on the response and is used for ACL compatibility */
2853 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2854 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2855 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2856 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2857
2858 /* shdr is valid only on the response and is used for ACL compatibility */
2859 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2860 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2861 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2862 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2863
2864 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2865 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2866 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2867 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2868 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2869 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2870 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2871 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2872 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2873 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2874 { /* END */ },
2875}};
2876
Willy Tarreau0108d902018-11-25 19:14:37 +01002877INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002878
2879/*
2880 * Local variables:
2881 * c-indent-level: 8
2882 * c-basic-offset: 8
2883 * End:
2884 */