blob: 5f579d1f1a79f1ac1e4012f91f056f98f6b9635f [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
2 * HTTP samples fetching
3 *
4 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/types.h>
14
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
18
19#include <common/base64.h>
20#include <common/chunk.h>
21#include <common/compat.h>
22#include <common/config.h>
23#include <common/debug.h>
24#include <common/http.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010025#include <common/initcall.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020026#include <common/memory.h>
27#include <common/standard.h>
28#include <common/version.h>
29
30#include <types/global.h>
31
32#include <proto/arg.h>
33#include <proto/auth.h>
34#include <proto/http_fetch.h>
Christopher Fauletef453ed2018-10-24 21:39:27 +020035#include <proto/http_htx.h>
36#include <proto/htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020037#include <proto/log.h>
38#include <proto/obj_type.h>
39#include <proto/proto_http.h>
40#include <proto/sample.h>
41#include <proto/stream.h>
42
43
44/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
45static THREAD_LOCAL struct hdr_ctx static_hdr_ctx;
Christopher Fauletef453ed2018-10-24 21:39:27 +020046static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
47
Willy Tarreau79e57332018-10-02 16:01:16 +020048
49/*
50 * Returns the data from Authorization header. Function may be called more
51 * than once so data is stored in txn->auth_data. When no header is found
52 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
53 * searching again for something we are unable to find anyway. However, if
54 * the result if valid, the cache is not reused because we would risk to
55 * have the credentials overwritten by another stream in parallel.
56 */
57
Christopher Faulet311c7ea2018-10-24 21:41:55 +020058static int get_http_auth(struct sample *smp)
Willy Tarreau79e57332018-10-02 16:01:16 +020059{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020060 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020061 struct http_txn *txn = s->txn;
62 struct buffer auth_method;
Willy Tarreau79e57332018-10-02 16:01:16 +020063 char *h, *p;
64 int len;
65
66#ifdef DEBUG_AUTH
67 printf("Auth for stream %p: %d\n", s, txn->auth.method);
68#endif
Willy Tarreau79e57332018-10-02 16:01:16 +020069 if (txn->auth.method == HTTP_AUTH_WRONG)
70 return 0;
71
72 txn->auth.method = HTTP_AUTH_WRONG;
73
Christopher Faulet311c7ea2018-10-24 21:41:55 +020074 if (IS_HTX_STRM(s) || (smp->px->mode == PR_MODE_TCP)) {
75 /* HTX version */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010076 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet311c7ea2018-10-24 21:41:55 +020077 struct http_hdr_ctx ctx = { .blk = NULL };
78 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020079
Christopher Faulet311c7ea2018-10-24 21:41:55 +020080 if (txn->flags & TX_USE_PX_CONN)
81 hdr = ist("Proxy-Authorization");
82 else
83 hdr = ist("Authorization");
84
Christopher Faulet311c7ea2018-10-24 21:41:55 +020085 ctx.blk = NULL;
86 if (!http_find_header(htx, hdr, &ctx, 0))
87 return 0;
88
89 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
90 len = p - ctx.value.ptr;
91 if (!p || len <= 0)
92 return 0;
93
94 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
95 return 0;
96
97 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +020098 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +020099 else {
100 /* LEGACY version */
101 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau79e57332018-10-02 16:01:16 +0200102
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200103 if (txn->flags & TX_USE_PX_CONN) {
104 h = "Proxy-Authorization";
105 len = strlen(h);
106 } else {
107 h = "Authorization";
108 len = strlen(h);
109 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200110
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200111 if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx))
112 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200113
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200114 h = ctx.line + ctx.val;
Willy Tarreau79e57332018-10-02 16:01:16 +0200115
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200116 p = memchr(h, ' ', ctx.vlen);
117 len = p - h;
118 if (!p || len <= 0)
119 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200120
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200121 if (chunk_initlen(&auth_method, h, 0, len) != 1)
122 return 0;
123
124 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
125 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200126
127 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
128 struct buffer *http_auth = get_trash_chunk();
129
130 len = base64dec(txn->auth.method_data.area,
131 txn->auth.method_data.data,
132 http_auth->area, global.tune.bufsize - 1);
133
134 if (len < 0)
135 return 0;
136
137
138 http_auth->area[len] = '\0';
139
140 p = strchr(http_auth->area, ':');
141
142 if (!p)
143 return 0;
144
145 txn->auth.user = http_auth->area;
146 *p = '\0';
147 txn->auth.pass = p+1;
148
149 txn->auth.method = HTTP_AUTH_BASIC;
150 return 1;
151 }
152
153 return 0;
154}
155
156/* This function ensures that the prerequisites for an L7 fetch are ready,
157 * which means that a request or response is ready. If some data is missing,
158 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Fauletef453ed2018-10-24 21:39:27 +0200159 * to extract data from L7.
160 *
161 * The function returns :
162 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
163 * decide whether or not an HTTP message is present ;
164 * NULL if the requested data cannot be fetched or if it is certain that
165 * we'll never have any HTTP message there ;
166 * The HTX message if ready
167 */
168struct htx *smp_prefetch_htx(struct sample *smp, const struct arg *args)
169{
170 struct proxy *px = smp->px;
171 struct stream *s = smp->strm;
172 unsigned int opt = smp->opt;
173 struct http_txn *txn = NULL;
174 struct htx *htx = NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100175 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200176
177 /* Note: it is possible that <s> is NULL when called before stream
178 * initialization (eg: tcp-request connection), so this function is the
179 * one responsible for guarding against this case for all HTTP users.
180 */
181 if (!s)
182 return NULL;
183
184 if (!s->txn) {
185 if (unlikely(!http_alloc_txn(s)))
186 return NULL; /* not enough memory */
187 http_init_txn(s);
188 txn = s->txn;
189 }
190
191 if (px->mode == PR_MODE_HTTP) {
192 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100193 htx = htxbuf(&s->req.buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200194 if (htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH) {
195 /* Parsing is done by the mux, just wait */
196 smp->flags |= SMP_F_MAY_CHANGE;
197 return NULL;
198 }
199
200 /* OK we just got a valid HTTP request. We have some
201 * minor preparation to perform so that further checks
202 * can rely on HTTP tests.
203 */
204 if (txn) {
205 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100206 txn->meth = sl->info.req.meth;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200207 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
208 s->flags |= SF_REDIRECTABLE;
209 }
210
211 /* otherwise everything's ready for the request */
212 }
213 else {
Christopher Faulet27ba2dc2018-12-05 11:53:24 +0100214 htx = htxbuf(&s->res.buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200215 if (htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH) {
216 /* Parsing is done by the mux, just wait */
217 smp->flags |= SMP_F_MAY_CHANGE;
218 return NULL;
219 }
220 }
221 }
222 else { /* PR_MODE_TCP */
223 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
224 struct buffer *buf;
225 struct h1m h1m;
226 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100227 union h1_sl h1sl;
228 unsigned int flags = HTX_FL_NONE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200229 int ret;
230
231 buf = &s->req.buf;
232 if (b_head(buf) + b_data(buf) > b_wrap(buf))
233 b_slow_realign(buf, trash.area, 0);
234
235 h1m_init_req(&h1m);
236 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100237 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200238 if (ret <= 0) {
239 /* Invalid or too big*/
240 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
241 return NULL;
242
243 /* wait for a full request */
244 smp->flags |= SMP_F_MAY_CHANGE;
245 return NULL;
246 }
247
248 /* OK we just got a valid HTTP request. We have to
249 * convert it into an HTX message.
250 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100251 if (unlikely(h1sl.rq.v.len == 0)) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200252 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100253 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200254 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100255 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200256 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100257 else if ((h1sl.rq.v.len == 8) &&
258 ((*(h1sl.rq.v.ptr + 5) > '1') ||
259 ((*(h1sl.rq.v.ptr + 5) == '1') && (*(h1sl.rq.v.ptr + 7) >= '1'))))
260 h1m.flags |= H1_MF_VER_11;
261
262
263 /* Set HTX start-line flags */
264 if (h1m.flags & H1_MF_VER_11)
265 flags |= HTX_SL_F_VER_11;
266 if (h1m.flags & H1_MF_XFER_ENC)
267 flags |= HTX_SL_F_XFER_ENC;
268 if (h1m.flags & H1_MF_XFER_LEN) {
269 flags |= HTX_SL_F_XFER_LEN;
270 if (h1m.flags & H1_MF_CHNK)
271 flags |= HTX_SL_F_CHNK;
272 else if (h1m.flags & H1_MF_CLEN)
273 flags |= HTX_SL_F_CLEN;
274 }
275
Christopher Fauletef453ed2018-10-24 21:39:27 +0200276 htx = htx_from_buf(get_trash_chunk());
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100277 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
278 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200279 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100280 sl->info.req.meth = h1sl.rq.meth;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200281
282 if (txn) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100283 txn->meth = h1sl.rq.meth;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200284 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
285 s->flags |= SF_REDIRECTABLE;
286 }
287 /* Ok, now everything's ready for the request */
288 }
289 else {
290 /* Impossible, no HTTP fetch on tcp-response */
291 return NULL;
292 }
293 }
294
295 /* everything's OK */
296 smp->data.u.sint = 1;
297 return htx;
298}
299
300/* This function ensures that the prerequisites for an L7 fetch are ready,
301 * which means that a request or response is ready. If some data is missing,
302 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau79e57332018-10-02 16:01:16 +0200303 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
304 * another test is made to ensure the required information is not gone.
305 *
306 * The function returns :
307 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
308 * decide whether or not an HTTP message is present ;
309 * 0 if the requested data cannot be fetched or if it is certain that
310 * we'll never have any HTTP message there ;
311 * 1 if an HTTP message is ready
312 */
313int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
314 const struct arg *args, struct sample *smp, int req_vol)
315{
316 struct http_txn *txn;
317 struct http_msg *msg;
318
319 /* Note: it is possible that <s> is NULL when called before stream
320 * initialization (eg: tcp-request connection), so this function is the
321 * one responsible for guarding against this case for all HTTP users.
322 */
323 if (!s)
324 return 0;
325
326 if (!s->txn) {
327 if (unlikely(!http_alloc_txn(s)))
328 return 0; /* not enough memory */
329 http_init_txn(s);
330 }
331 txn = s->txn;
332 msg = &txn->req;
333
334 /* Check for a dependency on a request */
335 smp->data.type = SMP_T_BOOL;
336
337 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
338 /* If the buffer does not leave enough free space at the end,
339 * we must first realign it.
340 */
341 if (ci_head(&s->req) > b_orig(&s->req.buf) &&
342 ci_head(&s->req) + ci_data(&s->req) > b_wrap(&s->req.buf) - global.tune.maxrewrite)
343 channel_slow_realign(&s->req, trash.area);
344
345 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
346 if (msg->msg_state == HTTP_MSG_ERROR)
347 return 0;
348
349 /* Try to decode HTTP request */
350 if (likely(msg->next < ci_data(&s->req)))
351 http_msg_analyzer(msg, &txn->hdr_idx);
352
353 /* Still no valid request ? */
354 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
355 if ((msg->msg_state == HTTP_MSG_ERROR) ||
356 channel_full(&s->req, global.tune.maxrewrite)) {
357 return 0;
358 }
359 /* wait for final state */
360 smp->flags |= SMP_F_MAY_CHANGE;
361 return 0;
362 }
363
364 /* OK we just got a valid HTTP request. We have some minor
365 * preparation to perform so that further checks can rely
366 * on HTTP tests.
367 */
368
369 /* If the request was parsed but was too large, we must absolutely
370 * return an error so that it is not processed. At the moment this
371 * cannot happen, but if the parsers are to change in the future,
372 * we want this check to be maintained.
373 */
374 if (unlikely(ci_head(&s->req) + ci_data(&s->req) >
375 b_wrap(&s->req.buf) - global.tune.maxrewrite)) {
376 msg->err_state = msg->msg_state;
377 msg->msg_state = HTTP_MSG_ERROR;
378 smp->data.u.sint = 1;
379 return 1;
380 }
381
382 txn->meth = find_http_meth(ci_head(msg->chn), msg->sl.rq.m_l);
383 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
384 s->flags |= SF_REDIRECTABLE;
385
386 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
387 return 0;
388 }
389
390 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
391 return 0; /* data might have moved and indexes changed */
392 }
393
394 /* otherwise everything's ready for the request */
395 }
396 else {
397 /* Check for a dependency on a response */
398 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
399 smp->flags |= SMP_F_MAY_CHANGE;
400 return 0;
401 }
402 }
403
404 /* everything's OK */
405 smp->data.u.sint = 1;
406 return 1;
407}
408
409/* This function fetches the method of current HTTP request and stores
410 * it in the global pattern struct as a chunk. There are two possibilities :
411 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
412 * in <len> and <ptr> is NULL ;
413 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
414 * <len> to its length.
415 * This is intended to be used with pat_match_meth() only.
416 */
417static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
418{
419 int meth;
420 struct http_txn *txn;
421
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200422 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
423 /* HTX version */
424 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +0200425
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200426 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +0200427 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200428
429 txn = smp->strm->txn;
430 meth = txn->meth;
431 smp->data.type = SMP_T_METH;
432 smp->data.u.meth.meth = meth;
433 if (meth == HTTP_METH_OTHER) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100434 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200435
436 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
437 /* ensure the indexes are not affected */
438 return 0;
439
440 sl = http_find_stline(htx);
441 smp->flags |= SMP_F_CONST;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100442 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
443 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200444 }
445 smp->flags |= SMP_F_VOL_1ST;
446 }
447 else {
448 /* LEGACY version */
449 CHECK_HTTP_MESSAGE_FIRST_PERM();
450
451 txn = smp->strm->txn;
452 meth = txn->meth;
453 smp->data.type = SMP_T_METH;
454 smp->data.u.meth.meth = meth;
455 if (meth == HTTP_METH_OTHER) {
456 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
457 /* ensure the indexes are not affected */
458 return 0;
459 smp->flags |= SMP_F_CONST;
460 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
461 smp->data.u.meth.str.area = ci_head(txn->req.chn);
462 }
463 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200464 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200465 return 1;
466}
467
468static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
469{
470 struct http_txn *txn;
471 char *ptr;
472 int len;
473
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200474 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
475 /* HTX version */
476 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100477 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200478
479 if (!htx)
480 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200481
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200482 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100483 len = HTX_SL_REQ_VLEN(sl);
484 ptr = HTX_SL_REQ_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200485 }
486 else {
487 /* LEGACY version */
488 CHECK_HTTP_MESSAGE_FIRST();
489
490 txn = smp->strm->txn;
491 len = txn->req.sl.rq.v_l;
492 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.v;
493 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200494
495 while ((len-- > 0) && (*ptr++ != '/'));
496 if (len <= 0)
497 return 0;
498
499 smp->data.type = SMP_T_STR;
500 smp->data.u.str.area = ptr;
501 smp->data.u.str.data = len;
502
503 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
504 return 1;
505}
506
507static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
508{
509 struct http_txn *txn;
510 char *ptr;
511 int len;
512
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200513 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
514 /* HTX version */
515 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100516 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200517
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200518 if (!htx)
519 return 0;
520
521 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100522 len = HTX_SL_RES_VLEN(sl);
523 ptr = HTX_SL_RES_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200524 }
525 else {
526 /* LEGACY version */
527 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +0200528
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200529 txn = smp->strm->txn;
530 if (txn->rsp.msg_state < HTTP_MSG_BODY)
531 return 0;
532
533 len = txn->rsp.sl.st.v_l;
534 ptr = ci_head(txn->rsp.chn);
535 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200536
537 while ((len-- > 0) && (*ptr++ != '/'));
538 if (len <= 0)
539 return 0;
540
541 smp->data.type = SMP_T_STR;
542 smp->data.u.str.area = ptr;
543 smp->data.u.str.data = len;
544
545 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
546 return 1;
547}
548
549/* 3. Check on Status Code. We manipulate integers here. */
550static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
551{
552 struct http_txn *txn;
553 char *ptr;
554 int len;
555
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200556 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
557 /* HTX version */
558 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100559 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200560
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200561 if (!htx)
562 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200563
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200564 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100565 len = HTX_SL_RES_CLEN(sl);
566 ptr = HTX_SL_RES_CPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200567 }
568 else {
569 /* LEGACY version */
570 CHECK_HTTP_MESSAGE_FIRST();
571
572 txn = smp->strm->txn;
573 if (txn->rsp.msg_state < HTTP_MSG_BODY)
574 return 0;
575
576 len = txn->rsp.sl.st.c_l;
577 ptr = ci_head(txn->rsp.chn) + txn->rsp.sl.st.c;
578 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200579
580 smp->data.type = SMP_T_SINT;
581 smp->data.u.sint = __strl2ui(ptr, len);
582 smp->flags = SMP_F_VOL_1ST;
583 return 1;
584}
585
586static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
587{
588 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
589 return 0;
590
591 if (!smp->strm->unique_id) {
592 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
593 return 0;
594 smp->strm->unique_id[0] = '\0';
595 }
596 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
597 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
598
599 smp->data.type = SMP_T_STR;
600 smp->data.u.str.area = smp->strm->unique_id;
601 smp->flags = SMP_F_CONST;
602 return 1;
603}
604
605/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800606 * empty line which separes headers from the body. This is useful
607 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200608 */
609static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
610{
Willy Tarreau79e57332018-10-02 16:01:16 +0200611 struct http_txn *txn;
612
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200613 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
614 /* HTX version */
615 struct htx *htx = smp_prefetch_htx(smp, args);
616 struct buffer *temp;
617 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200618
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200619 if (!htx)
620 return 0;
621 temp = get_trash_chunk();
622 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
623 struct htx_blk *blk = htx_get_blk(htx, pos);
624 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200625
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200626 if (type == HTX_BLK_HDR) {
627 struct ist n = htx_get_blk_name(htx, blk);
628 struct ist v = htx_get_blk_value(htx, blk);
629
Christopher Fauletc59ff232018-12-03 13:58:44 +0100630 if (!htx_hdr_to_h1(n, v, temp))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200631 return 0;
632 }
633 else if (type == HTX_BLK_EOH) {
634 if (!chunk_memcat(temp, "\r\n", 2))
635 return 0;
636 break;
637 }
638 }
639 smp->data.type = SMP_T_STR;
640 smp->data.u.str = *temp;
641
642 }
643 else {
644 /* LEGACY version */
645 struct http_msg *msg;
646 struct hdr_idx *idx;
647
648 CHECK_HTTP_MESSAGE_FIRST();
649
650 txn = smp->strm->txn;
651 idx = &txn->hdr_idx;
652 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200653
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200654 smp->data.type = SMP_T_STR;
655 smp->data.u.str.area = ci_head(msg->chn) + hdr_idx_first_pos(idx);
656 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
657 (ci_head(msg->chn)[msg->eoh] == '\r');
658 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200659 return 1;
660}
661
662/* Returns the header request in a length/value encoded format.
663 * This is useful for exchanges with the SPOE.
664 *
665 * A "length value" is a multibyte code encoding numbers. It uses the
666 * SPOE format. The encoding is the following:
667 *
668 * Each couple "header name" / "header value" is composed
669 * like this:
670 * "length value" "header name bytes"
671 * "length value" "header value bytes"
672 * When the last header is reached, the header name and the header
673 * value are empty. Their length are 0
674 */
675static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
676{
Willy Tarreau79e57332018-10-02 16:01:16 +0200677 struct http_txn *txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200678 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200679
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200680 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
681 /* HTX version */
682 struct htx *htx = smp_prefetch_htx(smp, args);
683 struct buffer *temp;
684 char *p, *end;
685 int32_t pos;
686 int ret;
Willy Tarreau79e57332018-10-02 16:01:16 +0200687
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200688 if (!htx)
689 return 0;
690 temp = get_trash_chunk();
691 p = temp->area;
692 end = temp->area + temp->size;
693 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
694 struct htx_blk *blk = htx_get_blk(htx, pos);
695 enum htx_blk_type type = htx_get_blk_type(blk);
696 struct ist n, v;
Willy Tarreau79e57332018-10-02 16:01:16 +0200697
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200698 if (type == HTX_BLK_HDR) {
699 n = htx_get_blk_name(htx,blk);
700 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200701
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200702 /* encode the header name. */
703 ret = encode_varint(n.len, &p, end);
704 if (ret == -1)
705 return 0;
706 if (p + n.len > end)
707 return 0;
708 memcpy(p, n.ptr, n.len);
709 p += n.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200710
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200711 /* encode the header value. */
712 ret = encode_varint(v.len, &p, end);
713 if (ret == -1)
714 return 0;
715 if (p + v.len > end)
716 return 0;
717 memcpy(p, v.ptr, v.len);
718 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200719
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200720 }
721 else if (type == HTX_BLK_EOH) {
722 /* encode the end of the header list with empty
723 * header name and header value.
724 */
725 ret = encode_varint(0, &p, end);
726 if (ret == -1)
727 return 0;
728 ret = encode_varint(0, &p, end);
729 if (ret == -1)
730 return 0;
731 break;
732 }
733 }
734
735 /* Initialise sample data which will be filled. */
736 smp->data.type = SMP_T_BIN;
737 smp->data.u.str.area = temp->area;
738 smp->data.u.str.data = p - temp->area;
739 smp->data.u.str.size = temp->size;
740 }
741 else {
742 /* LEGACY version */
743 struct http_msg *msg;
744 struct hdr_idx *idx;
745 const char *cur_ptr, *cur_next, *p;
746 int old_idx, cur_idx;
747 struct hdr_idx_elem *cur_hdr;
748 const char *hn, *hv;
749 int hnl, hvl;
750 int ret;
751 char *buf;
752 char *end;
753
754 CHECK_HTTP_MESSAGE_FIRST();
755
756 temp = get_trash_chunk();
757 buf = temp->area;
758 end = temp->area + temp->size;
759
760 txn = smp->strm->txn;
761 idx = &txn->hdr_idx;
762 msg = &txn->req;
763
764 /* Build array of headers. */
765 old_idx = 0;
766 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(idx);
767 while (1) {
768 cur_idx = idx->v[old_idx].next;
769 if (!cur_idx)
770 break;
771 old_idx = cur_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +0200772
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200773 cur_hdr = &idx->v[cur_idx];
774 cur_ptr = cur_next;
775 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
776
777 /* Now we have one full header at cur_ptr of len cur_hdr->len,
778 * and the next header starts at cur_next. We'll check
779 * this header in the list as well as against the default
780 * rule.
781 */
782
783 /* look for ': *'. */
784 hn = cur_ptr;
785 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
786 if (p >= cur_ptr+cur_hdr->len)
787 continue;
788 hnl = p - hn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200789 p++;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200790 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
791 p++;
792 if (p >= cur_ptr + cur_hdr->len)
793 continue;
794 hv = p;
795 hvl = cur_ptr + cur_hdr->len-p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200796
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200797 /* encode the header name. */
798 ret = encode_varint(hnl, &buf, end);
799 if (ret == -1)
800 return 0;
801 if (buf + hnl > end)
802 return 0;
803 memcpy(buf, hn, hnl);
804 buf += hnl;
805
806 /* encode and copy the value. */
807 ret = encode_varint(hvl, &buf, end);
808 if (ret == -1)
809 return 0;
810 if (buf + hvl > end)
811 return 0;
812 memcpy(buf, hv, hvl);
813 buf += hvl;
814 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200815
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200816 /* encode the end of the header list with empty
817 * header name and header value.
818 */
819 ret = encode_varint(0, &buf, end);
Willy Tarreau79e57332018-10-02 16:01:16 +0200820 if (ret == -1)
821 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200822 ret = encode_varint(0, &buf, end);
823 if (ret == -1)
Willy Tarreau79e57332018-10-02 16:01:16 +0200824 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200825
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200826 /* Initialise sample data which will be filled. */
827 smp->data.type = SMP_T_BIN;
828 smp->data.u.str.area = temp->area;
829 smp->data.u.str.data = buf - temp->area;
830 smp->data.u.str.size = temp->size;
831 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200832 return 1;
833}
834
835/* returns the longest available part of the body. This requires that the body
836 * has been waited for using http-buffer-request.
837 */
838static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
839{
Willy Tarreau79e57332018-10-02 16:01:16 +0200840 struct buffer *temp;
841
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200842 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
843 /* HTX version */
844 struct htx *htx = smp_prefetch_htx(smp, args);
845 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200846
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200847 if (!htx)
848 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200849
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200850 temp = get_trash_chunk();
851 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
852 struct htx_blk *blk = htx_get_blk(htx, pos);
853 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200854
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200855 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
856 break;
857 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +0100858 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200859 return 0;
860 }
861 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200862
Willy Tarreau79e57332018-10-02 16:01:16 +0200863 smp->data.type = SMP_T_BIN;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200864 smp->data.u.str = *temp;
865 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200866 }
867 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200868 /* LEGACY version */
869 struct http_msg *msg;
870 unsigned long len;
871 unsigned long block1;
872 char *body;
873
874 CHECK_HTTP_MESSAGE_FIRST();
875
876 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
877 msg = &smp->strm->txn->req;
878 else
879 msg = &smp->strm->txn->rsp;
880
881 len = http_body_bytes(msg);
882 body = c_ptr(msg->chn, -http_data_rewind(msg));
883
884 block1 = len;
885 if (block1 > b_wrap(&msg->chn->buf) - body)
886 block1 = b_wrap(&msg->chn->buf) - body;
887
888 if (block1 == len) {
889 /* buffer is not wrapped (or empty) */
890 smp->data.type = SMP_T_BIN;
891 smp->data.u.str.area = body;
892 smp->data.u.str.data = len;
893 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
894 }
895 else {
896 /* buffer is wrapped, we need to defragment it */
897 temp = get_trash_chunk();
898 memcpy(temp->area, body, block1);
899 memcpy(temp->area + block1, b_orig(&msg->chn->buf),
900 len - block1);
901 smp->data.type = SMP_T_BIN;
902 smp->data.u.str.area = temp->area;
903 smp->data.u.str.data = len;
904 smp->flags = SMP_F_VOL_TEST;
905 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200906 }
907 return 1;
908}
909
910
911/* returns the available length of the body. This requires that the body
912 * has been waited for using http-buffer-request.
913 */
914static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
915{
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200916 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
917 /* HTX version */
918 return 0; /* TODO: to be implemented */
919 }
920 else {
921 /* LEGACY version */
922 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200923
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200924 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +0200925
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200926 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
927 msg = &smp->strm->txn->req;
928 else
929 msg = &smp->strm->txn->rsp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200930
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200931 smp->data.type = SMP_T_SINT;
932 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreau79e57332018-10-02 16:01:16 +0200933
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200934 smp->flags = SMP_F_VOL_TEST;
935 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200936 return 1;
937}
938
939
940/* returns the advertised length of the body, or the advertised size of the
941 * chunks available in the buffer. This requires that the body has been waited
942 * for using http-buffer-request.
943 */
944static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
945{
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200946 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
947 /* HTX version */
948 return 0; /* TODO: to be implemented */
949 }
950 else {
951 /* LEGACY version */
952 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200953
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200954 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +0200955
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200956 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
957 msg = &smp->strm->txn->req;
958 else
959 msg = &smp->strm->txn->rsp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200960
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200961 smp->data.type = SMP_T_SINT;
962 smp->data.u.sint = msg->body_len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200963
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200964 smp->flags = SMP_F_VOL_TEST;
965 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200966 return 1;
967}
968
969
970/* 4. Check on URL/URI. A pointer to the URI is stored. */
971static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
972{
973 struct http_txn *txn;
974
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200975 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
976 /* HTX version */
977 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100978 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200979
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200980 if (!htx)
981 return 0;
982 sl = http_find_stline(htx);
983 smp->data.type = SMP_T_STR;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100984 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
985 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200986 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
987 }
988 else {
989 /* LEGACY version */
990 CHECK_HTTP_MESSAGE_FIRST();
991
992 txn = smp->strm->txn;
993 smp->data.type = SMP_T_STR;
994 smp->data.u.str.data = txn->req.sl.rq.u_l;
995 smp->data.u.str.area = ci_head(txn->req.chn) + txn->req.sl.rq.u;
996 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
997 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200998 return 1;
999}
1000
1001static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1002{
1003 struct http_txn *txn;
1004 struct sockaddr_storage addr;
1005
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001006 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1007 /* HTX version */
1008 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001009 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001010
1011 if (!htx)
1012 return 0;
1013 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001014 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001015 }
1016 else {
1017 /* LEGACY version */
1018 CHECK_HTTP_MESSAGE_FIRST();
1019
1020 txn = smp->strm->txn;
1021 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
1022 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001023
Willy Tarreau79e57332018-10-02 16:01:16 +02001024 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1025 return 0;
1026
1027 smp->data.type = SMP_T_IPV4;
1028 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
1029 smp->flags = 0;
1030 return 1;
1031}
1032
1033static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
1034{
1035 struct http_txn *txn;
1036 struct sockaddr_storage addr;
1037
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001038 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1039 /* HTX version */
1040 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001041 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001042
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001043 if (!htx)
1044 return 0;
1045 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001046 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001047 }
1048 else {
1049 /* LEGACY version */
1050 CHECK_HTTP_MESSAGE_FIRST();
1051
1052 txn = smp->strm->txn;
1053 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
1054 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001055 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1056 return 0;
1057
1058 smp->data.type = SMP_T_SINT;
1059 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
1060 smp->flags = 0;
1061 return 1;
1062}
1063
1064/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1065 * Accepts an optional argument of type string containing the header field name,
1066 * and an optional argument of type signed or unsigned integer to request an
1067 * explicit occurrence of the header. Note that in the event of a missing name,
1068 * headers are considered from the first one. It does not stop on commas and
1069 * returns full lines instead (useful for User-Agent or Date for example).
1070 */
1071static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1072{
Willy Tarreau79e57332018-10-02 16:01:16 +02001073 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001074
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001075 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1076 /* HTX version */
1077 struct htx *htx = smp_prefetch_htx(smp, args);
1078 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1079 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +02001080
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001081 if (!ctx) {
1082 /* first call */
1083 ctx = &static_http_hdr_ctx;
1084 ctx->blk = NULL;
1085 smp->ctx.a[0] = ctx;
1086 }
1087
1088 if (args) {
1089 if (args[0].type != ARGT_STR)
1090 return 0;
1091 name.ptr = args[0].data.str.area;
1092 name.len = args[0].data.str.data;
1093
1094 if (args[1].type == ARGT_SINT)
1095 occ = args[1].data.sint;
1096 }
1097
1098 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001099 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001100
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001101 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1102 /* search for header from the beginning */
1103 ctx->blk = NULL;
1104
1105 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1106 /* no explicit occurrence and single fetch => last header by default */
1107 occ = -1;
1108
1109 if (!occ)
1110 /* prepare to report multiple occurrences for ACL fetches */
1111 smp->flags |= SMP_F_NOT_LAST;
1112
1113 smp->data.type = SMP_T_STR;
1114 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1115 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1116 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001117 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001118 else {
1119 /* LEGACY version */
1120 struct hdr_idx *idx;
1121 struct hdr_ctx *ctx = smp->ctx.a[0];
1122 const struct http_msg *msg;
1123 const char *name_str = NULL;
1124 int name_len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001125
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001126 if (!ctx) {
1127 /* first call */
1128 ctx = &static_hdr_ctx;
1129 ctx->idx = 0;
1130 smp->ctx.a[0] = ctx;
1131 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001132
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001133 if (args) {
1134 if (args[0].type != ARGT_STR)
1135 return 0;
1136 name_str = args[0].data.str.area;
1137 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001138
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001139 if (args[1].type == ARGT_SINT)
1140 occ = args[1].data.sint;
1141 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001142
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001143 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001144
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001145 idx = &smp->strm->txn->hdr_idx;
1146 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 +02001147
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001148 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1149 /* search for header from the beginning */
1150 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001151
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001152 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1153 /* no explicit occurrence and single fetch => last header by default */
1154 occ = -1;
1155
1156 if (!occ)
1157 /* prepare to report multiple occurrences for ACL fetches */
1158 smp->flags |= SMP_F_NOT_LAST;
1159
1160 smp->data.type = SMP_T_STR;
1161 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1162 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1163 return 1;
1164 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001165 smp->flags &= ~SMP_F_NOT_LAST;
1166 return 0;
1167}
1168
1169/* 6. Check on HTTP header count. The number of occurrences is returned.
1170 * Accepts exactly 1 argument of type string. It does not stop on commas and
1171 * returns full lines instead (useful for User-Agent or Date for example).
1172 */
1173static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1174{
Willy Tarreau79e57332018-10-02 16:01:16 +02001175 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001176
1177 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1178 /* HTX version */
1179 struct htx *htx = smp_prefetch_htx(smp, args);
1180 struct http_hdr_ctx ctx;
1181 struct ist name;
1182
1183 if (!htx)
1184 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001185
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001186 if (args && args->type == ARGT_STR) {
1187 name.ptr = args->data.str.area;
1188 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001189 } else {
1190 name.ptr = NULL;
1191 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001192 }
1193
1194 ctx.blk = NULL;
1195 cnt = 0;
1196 while (http_find_header(htx, name, &ctx, 1))
1197 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001198 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001199 else {
1200 /* LEGACY version */
1201 struct hdr_idx *idx;
1202 struct hdr_ctx ctx;
1203 const struct http_msg *msg;
1204 const char *name = NULL;
1205 int len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001206
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001207 if (args && args->type == ARGT_STR) {
1208 name = args->data.str.area;
1209 len = args->data.str.data;
1210 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001211
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001212 CHECK_HTTP_MESSAGE_FIRST();
1213
1214 idx = &smp->strm->txn->hdr_idx;
1215 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 +02001216
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001217 ctx.idx = 0;
1218 cnt = 0;
1219 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
1220 cnt++;
1221 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001222
1223 smp->data.type = SMP_T_SINT;
1224 smp->data.u.sint = cnt;
1225 smp->flags = SMP_F_VOL_HDR;
1226 return 1;
1227}
1228
1229static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
1230{
Willy Tarreau79e57332018-10-02 16:01:16 +02001231 struct buffer *temp;
1232 char del = ',';
1233
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001234 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1235 /* HTX version */
1236 struct htx *htx = smp_prefetch_htx(smp, args);
1237 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001238
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001239 if (!htx)
1240 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001241
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001242 if (args && args->type == ARGT_STR)
1243 del = *args[0].data.str.area;
Willy Tarreau79e57332018-10-02 16:01:16 +02001244
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001245 temp = get_trash_chunk();
1246 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1247 struct htx_blk *blk = htx_get_blk(htx, pos);
1248 enum htx_blk_type type = htx_get_blk_type(blk);
1249 struct ist n;
Willy Tarreau79e57332018-10-02 16:01:16 +02001250
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001251 if (type == HTX_BLK_EOH)
1252 break;
1253 if (type != HTX_BLK_HDR)
1254 continue;
1255 n = htx_get_blk_name(htx, blk);
1256
1257 if (temp->data)
1258 temp->area[temp->data++] = del;
1259 chunk_memcat(temp, n.ptr, n.len);
1260 }
1261 }
1262 else {
1263 /* LEGACY version */
1264 struct hdr_idx *idx;
1265 struct hdr_ctx ctx;
1266 const struct http_msg *msg;
1267
1268 if (args && args->type == ARGT_STR)
1269 del = *args[0].data.str.area;
1270
1271 CHECK_HTTP_MESSAGE_FIRST();
1272
1273 idx = &smp->strm->txn->hdr_idx;
1274 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
1275
1276 temp = get_trash_chunk();
1277
1278 ctx.idx = 0;
1279 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
1280 if (temp->data)
1281 temp->area[temp->data++] = del;
1282 memcpy(temp->area + temp->data, ctx.line, ctx.del);
1283 temp->data += ctx.del;
1284 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001285 }
1286
1287 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001288 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001289 smp->flags = SMP_F_VOL_HDR;
1290 return 1;
1291}
1292
1293/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1294 * Accepts an optional argument of type string containing the header field name,
1295 * and an optional argument of type signed or unsigned integer to request an
1296 * explicit occurrence of the header. Note that in the event of a missing name,
1297 * headers are considered from the first one.
1298 */
1299static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1300{
Willy Tarreau79e57332018-10-02 16:01:16 +02001301 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001302
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001303 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1304 /* HTX version */
1305 struct htx *htx = smp_prefetch_htx(smp, args);
1306 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1307 struct ist name;
1308
1309 if (!ctx) {
1310 /* first call */
1311 ctx = &static_http_hdr_ctx;
1312 ctx->blk = NULL;
1313 smp->ctx.a[0] = ctx;
1314 }
1315
1316 if (args) {
1317 if (args[0].type != ARGT_STR)
1318 return 0;
1319 name.ptr = args[0].data.str.area;
1320 name.len = args[0].data.str.data;
1321
1322 if (args[1].type == ARGT_SINT)
1323 occ = args[1].data.sint;
1324 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001325
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001326 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001327 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001328
1329 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1330 /* search for header from the beginning */
1331 ctx->blk = NULL;
1332
1333 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1334 /* no explicit occurrence and single fetch => last header by default */
1335 occ = -1;
1336
1337 if (!occ)
1338 /* prepare to report multiple occurrences for ACL fetches */
1339 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001340
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001341 smp->data.type = SMP_T_STR;
1342 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1343 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1344 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001345 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001346 else {
1347 /* LEGACY version */
1348 struct hdr_idx *idx;
1349 struct hdr_ctx *ctx = smp->ctx.a[0];
1350 const struct http_msg *msg;
1351 const char *name_str = NULL;
1352 int name_len = 0;
1353
1354 if (!ctx) {
1355 /* first call */
1356 ctx = &static_hdr_ctx;
1357 ctx->idx = 0;
1358 smp->ctx.a[0] = ctx;
1359 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001360
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001361 if (args) {
1362 if (args[0].type != ARGT_STR)
1363 return 0;
1364 name_str = args[0].data.str.area;
1365 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001366
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001367 if (args[1].type == ARGT_SINT)
1368 occ = args[1].data.sint;
1369 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001370
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001371 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001372
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001373 idx = &smp->strm->txn->hdr_idx;
1374 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 +02001375
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001376 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1377 /* search for header from the beginning */
1378 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001379
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001380 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1381 /* no explicit occurrence and single fetch => last header by default */
1382 occ = -1;
1383
1384 if (!occ)
1385 /* prepare to report multiple occurrences for ACL fetches */
1386 smp->flags |= SMP_F_NOT_LAST;
1387
1388 smp->data.type = SMP_T_STR;
1389 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1390 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1391 return 1;
1392 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001393
1394 smp->flags &= ~SMP_F_NOT_LAST;
1395 return 0;
1396}
1397
1398/* 6. Check on HTTP header count. The number of occurrences is returned.
1399 * Accepts exactly 1 argument of type string.
1400 */
1401static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1402{
Willy Tarreau79e57332018-10-02 16:01:16 +02001403 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001404
1405 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1406 /* HTX version */
1407 struct htx *htx = smp_prefetch_htx(smp, args);
1408 struct http_hdr_ctx ctx;
1409 struct ist name;
1410
1411 if (!htx)
1412 return 0;
1413
1414 if (args && args->type == ARGT_STR) {
1415 name.ptr = args->data.str.area;
1416 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001417 } else {
1418 name.ptr = NULL;
1419 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001420 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001421
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001422 ctx.blk = NULL;
1423 cnt = 0;
1424 while (http_find_header(htx, name, &ctx, 0))
1425 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001426 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001427 else {
1428 /* LEGACY version */
1429 struct hdr_idx *idx;
1430 struct hdr_ctx ctx;
1431 const struct http_msg *msg;
1432 const char *name = NULL;
1433 int len = 0;
1434
1435 if (args && args->type == ARGT_STR) {
1436 name = args->data.str.area;
1437 len = args->data.str.data;
1438 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001439
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001440 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001441
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001442 idx = &smp->strm->txn->hdr_idx;
1443 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 +02001444
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001445 ctx.idx = 0;
1446 cnt = 0;
1447 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
1448 cnt++;
1449 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001450
1451 smp->data.type = SMP_T_SINT;
1452 smp->data.u.sint = cnt;
1453 smp->flags = SMP_F_VOL_HDR;
1454 return 1;
1455}
1456
1457/* Fetch an HTTP header's integer value. The integer value is returned. It
1458 * takes a mandatory argument of type string and an optional one of type int
1459 * to designate a specific occurrence. It returns an unsigned integer, which
1460 * may or may not be appropriate for everything.
1461 */
1462static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1463{
1464 int ret = smp_fetch_hdr(args, smp, kw, private);
1465
1466 if (ret > 0) {
1467 smp->data.type = SMP_T_SINT;
1468 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1469 smp->data.u.str.data);
1470 }
1471
1472 return ret;
1473}
1474
1475/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1476 * and an optional one of type int to designate a specific occurrence.
1477 * It returns an IPv4 or IPv6 address.
1478 */
1479static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1480{
1481 int ret;
1482
1483 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
1484 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
1485 smp->data.type = SMP_T_IPV4;
1486 break;
1487 } else {
1488 struct buffer *temp = get_trash_chunk();
1489 if (smp->data.u.str.data < temp->size - 1) {
1490 memcpy(temp->area, smp->data.u.str.area,
1491 smp->data.u.str.data);
1492 temp->area[smp->data.u.str.data] = '\0';
1493 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1494 smp->data.type = SMP_T_IPV6;
1495 break;
1496 }
1497 }
1498 }
1499
1500 /* if the header doesn't match an IP address, fetch next one */
1501 if (!(smp->flags & SMP_F_NOT_LAST))
1502 return 0;
1503 }
1504 return ret;
1505}
1506
1507/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1508 * the first '/' after the possible hostname, and ends before the possible '?'.
1509 */
1510static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1511{
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001512 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1513 /* HTX version */
1514 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001515 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001516 struct ist path;
1517 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001518
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001519 if (!htx)
1520 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001521
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001522 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001523 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001524 if (!path.ptr)
1525 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001526
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001527 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Willy Tarreau79e57332018-10-02 16:01:16 +02001528
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001529 /* OK, we got the '/' ! */
1530 smp->data.type = SMP_T_STR;
1531 smp->data.u.str.area = path.ptr;
1532 smp->data.u.str.data = len;
1533 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1534 }
1535 else {
1536 struct http_txn *txn;
1537 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001538
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001539 CHECK_HTTP_MESSAGE_FIRST();
1540
1541 txn = smp->strm->txn;
1542 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1543 ptr = http_txn_get_path(txn);
1544 if (!ptr)
1545 return 0;
1546
1547 /* OK, we got the '/' ! */
1548 smp->data.type = SMP_T_STR;
1549 smp->data.u.str.area = ptr;
1550
1551 while (ptr < end && *ptr != '?')
1552 ptr++;
1553
1554 smp->data.u.str.data = ptr - smp->data.u.str.area;
1555 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1556 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001557 return 1;
1558}
1559
1560/* This produces a concatenation of the first occurrence of the Host header
1561 * followed by the path component if it begins with a slash ('/'). This means
1562 * that '*' will not be added, resulting in exactly the first Host entry.
1563 * If no Host header is found, then the path is returned as-is. The returned
1564 * value is stored in the trash so it does not need to be marked constant.
1565 * The returned sample is of type string.
1566 */
1567static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1568{
Willy Tarreau79e57332018-10-02 16:01:16 +02001569 struct buffer *temp;
1570
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001571 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1572 /* HTX version */
1573 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001574 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001575 struct http_hdr_ctx ctx;
1576 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001577
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001578 if (!htx)
1579 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001580
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001581 ctx.blk = NULL;
1582 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1583 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001584
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001585 /* OK we have the header value in ctx.value */
1586 temp = get_trash_chunk();
1587 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1588
1589 /* now retrieve the path */
1590 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001591 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001592 if (path.ptr) {
1593 size_t len;
1594
1595 for (len = 0; len < path.len && *(path.ptr) != '?'; len++);
1596 if (len && *(path.ptr) == '/')
1597 chunk_memcat(temp, path.ptr, len);
1598 }
1599
1600 smp->data.type = SMP_T_STR;
1601 smp->data.u.str = *temp;
1602 }
1603 else {
1604 /* LEGACY version */
1605 struct http_txn *txn;
1606 char *ptr, *end, *beg;
1607 struct hdr_ctx ctx;
1608
1609 CHECK_HTTP_MESSAGE_FIRST();
1610
1611 txn = smp->strm->txn;
1612 ctx.idx = 0;
1613 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
1614 return smp_fetch_path(args, smp, kw, private);
1615
1616 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1617 temp = get_trash_chunk();
1618 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1619 smp->data.type = SMP_T_STR;
1620 smp->data.u.str.area = temp->area;
1621 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001622
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001623 /* now retrieve the path */
1624 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1625 beg = http_txn_get_path(txn);
1626 if (!beg)
1627 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001628
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001629 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1630
1631 if (beg < ptr && *beg == '/') {
1632 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1633 ptr - beg);
1634 smp->data.u.str.data += ptr - beg;
1635 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001636 }
1637
1638 smp->flags = SMP_F_VOL_1ST;
1639 return 1;
1640}
1641
1642/* This produces a 32-bit hash of the concatenation of the first occurrence of
1643 * the Host header followed by the path component if it begins with a slash ('/').
1644 * This means that '*' will not be added, resulting in exactly the first Host
1645 * entry. If no Host header is found, then the path is used. The resulting value
1646 * is hashed using the path hash followed by a full avalanche hash and provides a
1647 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1648 * high-traffic sites without having to store whole paths.
1649 */
1650static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1651{
Willy Tarreau79e57332018-10-02 16:01:16 +02001652 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001653
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001654 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1655 /* HTX version */
1656 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001657 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001658 struct http_hdr_ctx ctx;
1659 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001660
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001661 if (!htx)
1662 return 0;
1663
1664 ctx.blk = NULL;
1665 if (!http_find_header(htx, ist("Host"), &ctx, 0)) {
1666 /* OK we have the header value in ctx.value */
1667 while (ctx.value.len--)
1668 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1669 }
1670
1671 /* now retrieve the path */
1672 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001673 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001674 if (path.ptr) {
1675 size_t len;
1676
1677 for (len = 0; len < path.len && *(path.ptr) != '?'; len++);
1678 if (len && *(path.ptr) == '/') {
1679 while (len--)
1680 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1681 }
1682 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001683 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001684 else {
1685 /* LEGACY version */
1686 struct http_txn *txn;
1687 struct hdr_ctx ctx;
1688 char *ptr, *beg, *end;
1689 int len;
1690
1691 CHECK_HTTP_MESSAGE_FIRST();
1692
1693 txn = smp->strm->txn;
1694 ctx.idx = 0;
1695 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
1696 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1697 ptr = ctx.line + ctx.val;
1698 len = ctx.vlen;
1699 while (len--)
1700 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1701 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001702
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001703 /* now retrieve the path */
1704 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1705 beg = http_txn_get_path(txn);
1706 if (!beg)
1707 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001708
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001709 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001710
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001711 if (beg < ptr && *beg == '/') {
1712 while (beg < ptr)
1713 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
1714 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001715 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001716
Willy Tarreau79e57332018-10-02 16:01:16 +02001717 hash = full_hash(hash);
1718
1719 smp->data.type = SMP_T_SINT;
1720 smp->data.u.sint = hash;
1721 smp->flags = SMP_F_VOL_1ST;
1722 return 1;
1723}
1724
1725/* This concatenates the source address with the 32-bit hash of the Host and
1726 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1727 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1728 * on the source address length. The path hash is stored before the address so
1729 * that in environments where IPv6 is insignificant, truncating the output to
1730 * 8 bytes would still work.
1731 */
1732static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1733{
1734 struct buffer *temp;
1735 struct connection *cli_conn = objt_conn(smp->sess->origin);
1736
1737 if (!cli_conn)
1738 return 0;
1739
1740 if (!smp_fetch_base32(args, smp, kw, private))
1741 return 0;
1742
1743 temp = get_trash_chunk();
1744 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1745 temp->data += sizeof(unsigned int);
1746
1747 switch (cli_conn->addr.from.ss_family) {
1748 case AF_INET:
1749 memcpy(temp->area + temp->data,
1750 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1751 4);
1752 temp->data += 4;
1753 break;
1754 case AF_INET6:
1755 memcpy(temp->area + temp->data,
1756 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1757 16);
1758 temp->data += 16;
1759 break;
1760 default:
1761 return 0;
1762 }
1763
1764 smp->data.u.str = *temp;
1765 smp->data.type = SMP_T_BIN;
1766 return 1;
1767}
1768
1769/* Extracts the query string, which comes after the question mark '?'. If no
1770 * question mark is found, nothing is returned. Otherwise it returns a sample
1771 * of type string carrying the whole query string.
1772 */
1773static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1774{
Willy Tarreau79e57332018-10-02 16:01:16 +02001775 char *ptr, *end;
1776
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001777 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1778 /* HTX version */
1779 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001780 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001781
1782 if (!htx)
1783 return 0;
1784
1785 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001786 ptr = HTX_SL_REQ_UPTR(sl);
1787 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001788 }
1789 else {
1790 /* LEGACY version */
1791 struct http_txn *txn;
1792
1793 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001794
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001795 txn = smp->strm->txn;
1796 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.u;
1797 end = ptr + txn->req.sl.rq.u_l;
1798 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001799
1800 /* look up the '?' */
1801 do {
1802 if (ptr == end)
1803 return 0;
1804 } while (*ptr++ != '?');
1805
1806 smp->data.type = SMP_T_STR;
1807 smp->data.u.str.area = ptr;
1808 smp->data.u.str.data = end - ptr;
1809 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1810 return 1;
1811}
1812
1813static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1814{
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001815 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1816 /* HTX version */
1817 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +02001818
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001819 if (!htx)
1820 return 0;
1821 }
1822 else {
1823 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02001824
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001825 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
1826 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
1827 */
1828 CHECK_HTTP_MESSAGE_FIRST_PERM();
1829 }
1830 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001831 smp->data.u.sint = 1;
1832 return 1;
1833}
1834
1835/* return a valid test if the current request is the first one on the connection */
1836static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1837{
1838 smp->data.type = SMP_T_BOOL;
1839 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1840 return 1;
1841}
1842
1843/* Accepts exactly 1 argument of type userlist */
1844static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1845{
1846
1847 if (!args || args->type != ARGT_USR)
1848 return 0;
1849
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001850 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1851 /* HTX version */
1852 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +02001853
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001854 if (!htx)
1855 return 0;
1856 }
1857 else {
1858 /* LEGACY version */
1859 CHECK_HTTP_MESSAGE_FIRST();
1860 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001861
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001862 if (!get_http_auth(smp))
1863 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001864 smp->data.type = SMP_T_BOOL;
1865 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001866 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001867 return 1;
1868}
1869
1870/* Accepts exactly 1 argument of type userlist */
1871static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1872{
1873 if (!args || args->type != ARGT_USR)
1874 return 0;
1875
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001876 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1877 /* HTX version */
1878 struct htx *htx = smp_prefetch_htx(smp, args);
1879
1880 if (!htx)
1881 return 0;
1882 }
1883 else {
1884 /* LEGACY version */
1885 CHECK_HTTP_MESSAGE_FIRST();
1886 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001887
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001888 if (!get_http_auth(smp))
Willy Tarreau79e57332018-10-02 16:01:16 +02001889 return 0;
1890
1891 /* if the user does not belong to the userlist or has a wrong password,
1892 * report that it unconditionally does not match. Otherwise we return
1893 * a string containing the username.
1894 */
1895 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1896 smp->strm->txn->auth.pass))
1897 return 0;
1898
1899 /* pat_match_auth() will need the user list */
1900 smp->ctx.a[0] = args->data.usr;
1901
1902 smp->data.type = SMP_T_STR;
1903 smp->flags = SMP_F_CONST;
1904 smp->data.u.str.area = smp->strm->txn->auth.user;
1905 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1906
1907 return 1;
1908}
1909
1910/* Fetch a captured HTTP request header. The index is the position of
1911 * the "capture" option in the configuration file
1912 */
1913static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1914{
1915 struct proxy *fe = strm_fe(smp->strm);
1916 int idx;
1917
1918 if (!args || args->type != ARGT_SINT)
1919 return 0;
1920
1921 idx = args->data.sint;
1922
1923 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1924 return 0;
1925
1926 smp->data.type = SMP_T_STR;
1927 smp->flags |= SMP_F_CONST;
1928 smp->data.u.str.area = smp->strm->req_cap[idx];
1929 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1930
1931 return 1;
1932}
1933
1934/* Fetch a captured HTTP response header. The index is the position of
1935 * the "capture" option in the configuration file
1936 */
1937static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1938{
1939 struct proxy *fe = strm_fe(smp->strm);
1940 int idx;
1941
1942 if (!args || args->type != ARGT_SINT)
1943 return 0;
1944
1945 idx = args->data.sint;
1946
1947 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1948 return 0;
1949
1950 smp->data.type = SMP_T_STR;
1951 smp->flags |= SMP_F_CONST;
1952 smp->data.u.str.area = smp->strm->res_cap[idx];
1953 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1954
1955 return 1;
1956}
1957
1958/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1959static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1960{
1961 struct buffer *temp;
1962 struct http_txn *txn = smp->strm->txn;
1963 char *ptr;
1964
1965 if (!txn || !txn->uri)
1966 return 0;
1967
1968 ptr = txn->uri;
1969
1970 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1971 ptr++;
1972
1973 temp = get_trash_chunk();
1974 temp->area = txn->uri;
1975 temp->data = ptr - txn->uri;
1976 smp->data.u.str = *temp;
1977 smp->data.type = SMP_T_STR;
1978 smp->flags = SMP_F_CONST;
1979
1980 return 1;
1981
1982}
1983
1984/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1985static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1986{
1987 struct http_txn *txn = smp->strm->txn;
1988 struct ist path;
1989 const char *ptr;
1990
1991 if (!txn || !txn->uri)
1992 return 0;
1993
1994 ptr = txn->uri;
1995
1996 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1997 ptr++;
1998
1999 if (!*ptr)
2000 return 0;
2001
Christopher Faulet78337bb2018-11-15 14:35:18 +01002002 /* skip the first space and find space after URI */
2003 path = ist2(++ptr, 0);
2004 while (*ptr != ' ' && *ptr != '\0')
2005 ptr++;
2006 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002007
Christopher Faulet78337bb2018-11-15 14:35:18 +01002008 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02002009 if (!path.ptr)
2010 return 0;
2011
2012 smp->data.u.str.area = path.ptr;
2013 smp->data.u.str.data = path.len;
2014 smp->data.type = SMP_T_STR;
2015 smp->flags = SMP_F_CONST;
2016
2017 return 1;
2018}
2019
2020/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
2021 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2022 */
2023static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2024{
2025 struct http_txn *txn = smp->strm->txn;
2026
2027 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2028 return 0;
2029
2030 if (txn->req.flags & HTTP_MSGF_VER_11)
2031 smp->data.u.str.area = "HTTP/1.1";
2032 else
2033 smp->data.u.str.area = "HTTP/1.0";
2034
2035 smp->data.u.str.data = 8;
2036 smp->data.type = SMP_T_STR;
2037 smp->flags = SMP_F_CONST;
2038 return 1;
2039
2040}
2041
2042/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2043 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2044 */
2045static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2046{
2047 struct http_txn *txn = smp->strm->txn;
2048
2049 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2050 return 0;
2051
2052 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2053 smp->data.u.str.area = "HTTP/1.1";
2054 else
2055 smp->data.u.str.area = "HTTP/1.0";
2056
2057 smp->data.u.str.data = 8;
2058 smp->data.type = SMP_T_STR;
2059 smp->flags = SMP_F_CONST;
2060 return 1;
2061
2062}
2063
2064/* Iterate over all cookies present in a message. The context is stored in
2065 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2066 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2067 * the direction, multiple cookies may be parsed on the same line or not.
2068 * The cookie name is in args and the name length in args->data.str.len.
2069 * Accepts exactly 1 argument of type string. If the input options indicate
2070 * that no iterating is desired, then only last value is fetched if any.
2071 * The returned sample is of type CSTR. Can be used to parse cookies in other
2072 * files.
2073 */
2074static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2075{
Willy Tarreau79e57332018-10-02 16:01:16 +02002076 int occ = 0;
2077 int found = 0;
2078
2079 if (!args || args->type != ARGT_STR)
2080 return 0;
2081
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002082 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2083 /* HTX version */
2084 struct htx *htx = smp_prefetch_htx(smp, args);
2085 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2086 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002087
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002088 if (!ctx) {
2089 /* first call */
2090 ctx = &static_http_hdr_ctx;
2091 ctx->blk = NULL;
2092 smp->ctx.a[2] = ctx;
2093 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002094
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002095 if (!htx)
2096 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002097
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002098 hdr = (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2099 ? ist("Cookie")
2100 : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002101
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002102 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2103 /* no explicit occurrence and single fetch => last cookie by default */
2104 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02002105
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002106 /* OK so basically here, either we want only one value and it's the
2107 * last one, or we want to iterate over all of them and we fetch the
2108 * next one.
Willy Tarreau79e57332018-10-02 16:01:16 +02002109 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002110
2111 if (!(smp->flags & SMP_F_NOT_LAST)) {
2112 /* search for the header from the beginning, we must first initialize
2113 * the search parameters.
2114 */
2115 smp->ctx.a[0] = NULL;
2116 ctx->blk = NULL;
2117 }
2118
2119 smp->flags |= SMP_F_VOL_HDR;
2120 while (1) {
2121 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2122 if (!smp->ctx.a[0]) {
2123 if (!http_find_header(htx, hdr, ctx, 0))
2124 goto out;
2125
2126 if (ctx->value.len < args->data.str.data + 1)
2127 continue;
2128
2129 smp->ctx.a[0] = ctx->value.ptr;
2130 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2131 }
2132
2133 smp->data.type = SMP_T_STR;
2134 smp->flags |= SMP_F_CONST;
2135 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2136 args->data.str.area, args->data.str.data,
2137 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2138 &smp->data.u.str.area,
2139 &smp->data.u.str.data);
2140 if (smp->ctx.a[0]) {
2141 found = 1;
2142 if (occ >= 0) {
2143 /* one value was returned into smp->data.u.str.{str,len} */
2144 smp->flags |= SMP_F_NOT_LAST;
2145 return 1;
2146 }
2147 }
2148 /* if we're looking for last occurrence, let's loop */
2149 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002150 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002151 else {
2152 /* LEGACY version */
2153 struct http_txn *txn;
2154 struct hdr_idx *idx;
2155 struct hdr_ctx *ctx = smp->ctx.a[2];
2156 const struct http_msg *msg;
2157 const char *hdr_name;
2158 int hdr_name_len;
2159 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002160
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002161 if (!ctx) {
2162 /* first call */
2163 ctx = &static_hdr_ctx;
2164 ctx->idx = 0;
2165 smp->ctx.a[2] = ctx;
2166 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002167
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002168 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02002169
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002170 txn = smp->strm->txn;
2171 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +02002172
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002173 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
2174 msg = &txn->req;
2175 hdr_name = "Cookie";
2176 hdr_name_len = 6;
2177 } else {
2178 msg = &txn->rsp;
2179 hdr_name = "Set-Cookie";
2180 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002181 }
2182
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002183 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2184 /* no explicit occurrence and single fetch => last cookie by default */
2185 occ = -1;
2186
2187 /* OK so basically here, either we want only one value and it's the
2188 * last one, or we want to iterate over all of them and we fetch the
2189 * next one.
2190 */
2191
2192 sol = ci_head(msg->chn);
2193 if (!(smp->flags & SMP_F_NOT_LAST)) {
2194 /* search for the header from the beginning, we must first initialize
2195 * the search parameters.
2196 */
2197 smp->ctx.a[0] = NULL;
2198 ctx->idx = 0;
2199 }
2200
2201 smp->flags |= SMP_F_VOL_HDR;
2202
2203 while (1) {
2204 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2205 if (!smp->ctx.a[0]) {
2206 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2207 goto out;
2208
2209 if (ctx->vlen < args->data.str.data + 1)
2210 continue;
2211
2212 smp->ctx.a[0] = ctx->line + ctx->val;
2213 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2214 }
2215
2216 smp->data.type = SMP_T_STR;
2217 smp->flags |= SMP_F_CONST;
2218 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2219 args->data.str.area, args->data.str.data,
2220 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2221 &smp->data.u.str.area, &smp->data.u.str.data);
2222 if (smp->ctx.a[0]) {
2223 found = 1;
2224 if (occ >= 0) {
2225 /* one value was returned into smp->data.u.str.{str,len} */
2226 smp->flags |= SMP_F_NOT_LAST;
2227 return 1;
2228 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002229 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002230 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002231 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002232 }
2233 /* all cookie headers and values were scanned. If we're looking for the
2234 * last occurrence, we may return it now.
2235 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002236 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002237 smp->flags &= ~SMP_F_NOT_LAST;
2238 return found;
2239}
2240
2241/* Iterate over all cookies present in a request to count how many occurrences
2242 * match the name in args and args->data.str.len. If <multi> is non-null, then
2243 * multiple cookies may be parsed on the same line. The returned sample is of
2244 * type UINT. Accepts exactly 1 argument of type string.
2245 */
2246static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2247{
Willy Tarreau79e57332018-10-02 16:01:16 +02002248 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002249 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002250
2251 if (!args || args->type != ARGT_STR)
2252 return 0;
2253
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002254 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2255 /* HTX version */
2256 struct htx *htx = smp_prefetch_htx(smp, args);
2257 struct http_hdr_ctx ctx;
2258 struct ist hdr;
2259
2260 if (!htx)
2261 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002262
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002263 hdr = (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2264 ? ist("Cookie")
2265 : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002266
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002267 val_end = val_beg = NULL;
2268 ctx.blk = NULL;
2269 cnt = 0;
2270 while (1) {
2271 /* Note: val_beg == NULL every time we need to fetch a new header */
2272 if (!val_beg) {
2273 if (!http_find_header(htx, hdr, &ctx, 0))
2274 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002275
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002276 if (ctx.value.len < args->data.str.data + 1)
2277 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002278
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002279 val_beg = ctx.value.ptr;
2280 val_end = val_beg + ctx.value.len;
2281 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002282
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002283 smp->data.type = SMP_T_STR;
2284 smp->flags |= SMP_F_CONST;
2285 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2286 args->data.str.area, args->data.str.data,
2287 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2288 &smp->data.u.str.area,
2289 &smp->data.u.str.data))) {
2290 cnt++;
2291 }
2292 }
2293 }
2294 else {
2295 /* LEGACY version */
2296 struct http_txn *txn;
2297 struct hdr_idx *idx;
2298 struct hdr_ctx ctx;
2299 const struct http_msg *msg;
2300 const char *hdr_name;
2301 int hdr_name_len;
2302 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002303
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002304 CHECK_HTTP_MESSAGE_FIRST();
2305
2306 txn = smp->strm->txn;
2307 idx = &smp->strm->txn->hdr_idx;
2308
2309 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
2310 msg = &txn->req;
2311 hdr_name = "Cookie";
2312 hdr_name_len = 6;
2313 } else {
2314 msg = &txn->rsp;
2315 hdr_name = "Set-Cookie";
2316 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002317 }
2318
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002319 sol = ci_head(msg->chn);
2320 val_end = val_beg = NULL;
2321 ctx.idx = 0;
2322 cnt = 0;
2323
2324 while (1) {
2325 /* Note: val_beg == NULL every time we need to fetch a new header */
2326 if (!val_beg) {
2327 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2328 break;
2329
2330 if (ctx.vlen < args->data.str.data + 1)
2331 continue;
2332
2333 val_beg = ctx.line + ctx.val;
2334 val_end = val_beg + ctx.vlen;
2335 }
2336
2337 smp->data.type = SMP_T_STR;
2338 smp->flags |= SMP_F_CONST;
2339 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2340 args->data.str.area, args->data.str.data,
2341 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2342 &smp->data.u.str.area, &smp->data.u.str.data))) {
2343 cnt++;
2344 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002345 }
2346 }
2347
2348 smp->data.type = SMP_T_SINT;
2349 smp->data.u.sint = cnt;
2350 smp->flags |= SMP_F_VOL_HDR;
2351 return 1;
2352}
2353
2354/* Fetch an cookie's integer value. The integer value is returned. It
2355 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2356 */
2357static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2358{
2359 int ret = smp_fetch_cookie(args, smp, kw, private);
2360
2361 if (ret > 0) {
2362 smp->data.type = SMP_T_SINT;
2363 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2364 smp->data.u.str.data);
2365 }
2366
2367 return ret;
2368}
2369
2370/************************************************************************/
2371/* The code below is dedicated to sample fetches */
2372/************************************************************************/
2373
2374/* This scans a URL-encoded query string. It takes an optionally wrapping
2375 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2376 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2377 * pointers are updated for next iteration before leaving.
2378 */
2379static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2380{
2381 const char *vstart, *vend;
2382 struct buffer *temp;
2383 const char **chunks = (const char **)smp->ctx.a;
2384
2385 if (!http_find_next_url_param(chunks, name, name_len,
2386 &vstart, &vend, delim))
2387 return 0;
2388
2389 /* Create sample. If the value is contiguous, return the pointer as CONST,
2390 * if the value is wrapped, copy-it in a buffer.
2391 */
2392 smp->data.type = SMP_T_STR;
2393 if (chunks[2] &&
2394 vstart >= chunks[0] && vstart <= chunks[1] &&
2395 vend >= chunks[2] && vend <= chunks[3]) {
2396 /* Wrapped case. */
2397 temp = get_trash_chunk();
2398 memcpy(temp->area, vstart, chunks[1] - vstart);
2399 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2400 vend - chunks[2]);
2401 smp->data.u.str.area = temp->area;
2402 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2403 } else {
2404 /* Contiguous case. */
2405 smp->data.u.str.area = (char *)vstart;
2406 smp->data.u.str.data = vend - vstart;
2407 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2408 }
2409
2410 /* Update context, check wrapping. */
2411 chunks[0] = vend;
2412 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2413 chunks[1] = chunks[3];
2414 chunks[2] = NULL;
2415 }
2416
2417 if (chunks[0] < chunks[1])
2418 smp->flags |= SMP_F_NOT_LAST;
2419
2420 return 1;
2421}
2422
2423/* This function iterates over each parameter of the query string. It uses
2424 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2425 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2426 * An optional parameter name is passed in args[0], otherwise any parameter is
2427 * considered. It supports an optional delimiter argument for the beginning of
2428 * the string in args[1], which defaults to "?".
2429 */
2430static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2431{
Willy Tarreau79e57332018-10-02 16:01:16 +02002432 char delim = '?';
2433 const char *name;
2434 int name_len;
2435
2436 if (!args ||
2437 (args[0].type && args[0].type != ARGT_STR) ||
2438 (args[1].type && args[1].type != ARGT_STR))
2439 return 0;
2440
2441 name = "";
2442 name_len = 0;
2443 if (args->type == ARGT_STR) {
2444 name = args->data.str.area;
2445 name_len = args->data.str.data;
2446 }
2447
2448 if (args[1].type)
2449 delim = *args[1].data.str.area;
2450
2451 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002452 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2453 /* HTX version */
2454 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002455 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002456
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002457 if (!htx)
2458 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002459
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002460 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002461 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 +02002462 if (!smp->ctx.a[0])
2463 return 0;
2464
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002465 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002466 }
2467 else {
2468 /* LEGACY version */
2469 struct http_msg *msg;
2470
2471 CHECK_HTTP_MESSAGE_FIRST();
2472
2473 msg = &smp->strm->txn->req;
2474
2475 smp->ctx.a[0] = http_find_param_list(ci_head(msg->chn) + msg->sl.rq.u,
2476 msg->sl.rq.u_l, delim);
2477 if (!smp->ctx.a[0])
2478 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002479
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002480 smp->ctx.a[1] = ci_head(msg->chn) + msg->sl.rq.u + msg->sl.rq.u_l;
2481 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002482
2483 /* Assume that the context is filled with NULL pointer
2484 * before the first call.
2485 * smp->ctx.a[2] = NULL;
2486 * smp->ctx.a[3] = NULL;
2487 */
2488 }
2489
2490 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2491}
2492
2493/* This function iterates over each parameter of the body. This requires
2494 * that the body has been waited for using http-buffer-request. It uses
2495 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2496 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2497 * optional second part if the body wraps at the end of the buffer. An optional
2498 * parameter name is passed in args[0], otherwise any parameter is considered.
2499 */
2500static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2501{
Willy Tarreau79e57332018-10-02 16:01:16 +02002502 const char *name;
2503 int name_len;
2504
2505 if (!args || (args[0].type && args[0].type != ARGT_STR))
2506 return 0;
2507
2508 name = "";
2509 name_len = 0;
2510 if (args[0].type == ARGT_STR) {
2511 name = args[0].data.str.area;
2512 name_len = args[0].data.str.data;
2513 }
2514
2515 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002516 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2517 /* HTX version */
2518 struct htx *htx = smp_prefetch_htx(smp, args);
2519 struct buffer *temp;
2520 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002521
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002522 if (!htx)
2523 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002524
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002525 temp = get_trash_chunk();
2526 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
2527 struct htx_blk *blk = htx_get_blk(htx, pos);
2528 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002529
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002530 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
2531 break;
2532 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01002533 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002534 return 0;
2535 }
2536 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002537
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002538 smp->ctx.a[0] = temp->area;
2539 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002540
2541 /* Assume that the context is filled with NULL pointer
2542 * before the first call.
2543 * smp->ctx.a[2] = NULL;
2544 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002545 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002546 }
2547 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002548 /* LEGACY version */
2549 struct http_msg *msg;
2550 unsigned long len;
2551 unsigned long block1;
2552 char *body;
2553
2554 CHECK_HTTP_MESSAGE_FIRST();
2555
2556 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2557 msg = &smp->strm->txn->req;
2558 else
2559 msg = &smp->strm->txn->rsp;
2560
2561 len = http_body_bytes(msg);
2562 body = c_ptr(msg->chn, -http_data_rewind(msg));
2563
2564 block1 = len;
2565 if (block1 > b_wrap(&msg->chn->buf) - body)
2566 block1 = b_wrap(&msg->chn->buf) - body;
2567
2568 if (block1 == len) {
2569 /* buffer is not wrapped (or empty) */
2570 smp->ctx.a[0] = body;
2571 smp->ctx.a[1] = body + len;
2572
2573 /* Assume that the context is filled with NULL pointer
2574 * before the first call.
2575 * smp->ctx.a[2] = NULL;
2576 * smp->ctx.a[3] = NULL;
2577 */
2578 }
2579 else {
2580 /* buffer is wrapped, we need to defragment it */
2581 smp->ctx.a[0] = body;
2582 smp->ctx.a[1] = body + block1;
2583 smp->ctx.a[2] = b_orig(&msg->chn->buf);
2584 smp->ctx.a[3] = b_orig(&msg->chn->buf) + ( len - block1 );
2585 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002586 }
2587 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002588
Willy Tarreau79e57332018-10-02 16:01:16 +02002589 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2590}
2591
2592/* Return the signed integer value for the specified url parameter (see url_param
2593 * above).
2594 */
2595static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2596{
2597 int ret = smp_fetch_url_param(args, smp, kw, private);
2598
2599 if (ret > 0) {
2600 smp->data.type = SMP_T_SINT;
2601 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2602 smp->data.u.str.data);
2603 }
2604
2605 return ret;
2606}
2607
2608/* This produces a 32-bit hash of the concatenation of the first occurrence of
2609 * the Host header followed by the path component if it begins with a slash ('/').
2610 * This means that '*' will not be added, resulting in exactly the first Host
2611 * entry. If no Host header is found, then the path is used. The resulting value
2612 * is hashed using the url hash followed by a full avalanche hash and provides a
2613 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2614 * high-traffic sites without having to store whole paths.
2615 * this differs from the base32 functions in that it includes the url parameters
2616 * as well as the path
2617 */
2618static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2619{
Willy Tarreau79e57332018-10-02 16:01:16 +02002620 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002621
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002622 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2623 /* HTX version */
2624 struct htx *htx = smp_prefetch_htx(smp, args);
2625 struct http_hdr_ctx ctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002626 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002627 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002628
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002629 if (!htx)
2630 return 0;
2631
2632 ctx.blk = NULL;
2633 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2634 /* OK we have the header value in ctx.value */
2635 while (ctx.value.len--)
2636 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2637 }
2638
2639 /* now retrieve the path */
2640 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002641 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002642 while (path.len > 0 && *(path.ptr) != '?') {
2643 path.ptr++;
2644 path.len--;
2645 }
2646 if (path.len && *(path.ptr) == '/') {
2647 while (path.len--)
2648 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2649 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002650 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002651 else {
2652 /* LEGACY version */
2653 struct http_txn *txn;
2654 struct hdr_ctx ctx;
2655 char *ptr, *beg, *end;
2656 int len;
2657
2658 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02002659
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002660 txn = smp->strm->txn;
2661 ctx.idx = 0;
2662 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
2663 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2664 ptr = ctx.line + ctx.val;
2665 len = ctx.vlen;
2666 while (len--)
2667 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2668 }
2669
2670 /* now retrieve the path */
2671 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
2672 beg = http_txn_get_path(txn);
2673 if (!beg)
2674 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002675
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002676 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002677
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002678 if (beg < ptr && *beg == '/') {
2679 while (beg < ptr)
2680 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2681 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002682 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002683
Willy Tarreau79e57332018-10-02 16:01:16 +02002684 hash = full_hash(hash);
2685
2686 smp->data.type = SMP_T_SINT;
2687 smp->data.u.sint = hash;
2688 smp->flags = SMP_F_VOL_1ST;
2689 return 1;
2690}
2691
2692/* This concatenates the source address with the 32-bit hash of the Host and
2693 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2694 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2695 * on the source address length. The URL hash is stored before the address so
2696 * that in environments where IPv6 is insignificant, truncating the output to
2697 * 8 bytes would still work.
2698 */
2699static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2700{
2701 struct buffer *temp;
2702 struct connection *cli_conn = objt_conn(smp->sess->origin);
2703
2704 if (!cli_conn)
2705 return 0;
2706
2707 if (!smp_fetch_url32(args, smp, kw, private))
2708 return 0;
2709
2710 temp = get_trash_chunk();
2711 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2712 temp->data += sizeof(unsigned int);
2713
2714 switch (cli_conn->addr.from.ss_family) {
2715 case AF_INET:
2716 memcpy(temp->area + temp->data,
2717 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2718 4);
2719 temp->data += 4;
2720 break;
2721 case AF_INET6:
2722 memcpy(temp->area + temp->data,
2723 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2724 16);
2725 temp->data += 16;
2726 break;
2727 default:
2728 return 0;
2729 }
2730
2731 smp->data.u.str = *temp;
2732 smp->data.type = SMP_T_BIN;
2733 return 1;
2734}
2735
2736/************************************************************************/
2737/* Other utility functions */
2738/************************************************************************/
2739
2740/* This function is used to validate the arguments passed to any "hdr" fetch
2741 * keyword. These keywords support an optional positive or negative occurrence
2742 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2743 * is assumed that the types are already the correct ones. Returns 0 on error,
2744 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2745 * error message in case of error, that the caller is responsible for freeing.
2746 * The initial location must either be freeable or NULL.
2747 * Note: this function's pointer is checked from Lua.
2748 */
2749int val_hdr(struct arg *arg, char **err_msg)
2750{
2751 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2752 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2753 return 0;
2754 }
2755 return 1;
2756}
2757
2758/************************************************************************/
2759/* All supported sample fetch keywords must be declared here. */
2760/************************************************************************/
2761
2762/* Note: must not be declared <const> as its list will be overwritten */
2763static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2764 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2765 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2766 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2767
2768 /* capture are allocated and are permanent in the stream */
2769 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2770
2771 /* retrieve these captures from the HTTP logs */
2772 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2773 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2774 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2775
2776 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2777 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2778
2779 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2780 * are only here to match the ACL's name, are request-only and are used
2781 * for ACL compatibility only.
2782 */
2783 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2784 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
2785 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2786 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2787
2788 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2789 * only here to match the ACL's name, are request-only and are used for
2790 * ACL compatibility only.
2791 */
2792 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
2793 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2794 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2795 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2796
2797 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2798 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2799 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2800 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2801 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2802 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2803
2804 /* HTTP protocol on the request path */
2805 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2806 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2807
2808 /* HTTP version on the request path */
2809 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2810 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2811
2812 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2813 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2814 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2815 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2816
2817 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2818 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2819
2820 /* HTTP version on the response path */
2821 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2822 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2823
2824 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2825 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2826 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2827 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2828
2829 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2830 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2831 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2832 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2833 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2834 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2835 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2836
2837 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2838 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2839 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2840 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2841
2842 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2843 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2844 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2845 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2846 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2847 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2848 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2849
2850 /* scook is valid only on the response and is used for ACL compatibility */
2851 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2852 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2853 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2854 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2855
2856 /* shdr is valid only on the response and is used for ACL compatibility */
2857 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2858 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2859 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2860 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2861
2862 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2863 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2864 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2865 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2866 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2867 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2868 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2869 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2870 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2871 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2872 { /* END */ },
2873}};
2874
Willy Tarreau0108d902018-11-25 19:14:37 +01002875INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002876
2877/*
2878 * Local variables:
2879 * c-indent-level: 8
2880 * c-basic-offset: 8
2881 * End:
2882 */