blob: 884a32049718c776b6a6e8e947c5e4965c16f5e7 [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 */
76 struct htx *htx = htx_from_buf(&s->req.buf);
77 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
85 htx = htx_from_buf(&s->req.buf);
86 ctx.blk = NULL;
87 if (!http_find_header(htx, hdr, &ctx, 0))
88 return 0;
89
90 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
91 len = p - ctx.value.ptr;
92 if (!p || len <= 0)
93 return 0;
94
95 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
96 return 0;
97
98 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +020099 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200100 else {
101 /* LEGACY version */
102 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau79e57332018-10-02 16:01:16 +0200103
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200104 if (txn->flags & TX_USE_PX_CONN) {
105 h = "Proxy-Authorization";
106 len = strlen(h);
107 } else {
108 h = "Authorization";
109 len = strlen(h);
110 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200111
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200112 if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx))
113 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200114
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200115 h = ctx.line + ctx.val;
Willy Tarreau79e57332018-10-02 16:01:16 +0200116
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200117 p = memchr(h, ' ', ctx.vlen);
118 len = p - h;
119 if (!p || len <= 0)
120 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200121
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200122 if (chunk_initlen(&auth_method, h, 0, len) != 1)
123 return 0;
124
125 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
126 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200127
128 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
129 struct buffer *http_auth = get_trash_chunk();
130
131 len = base64dec(txn->auth.method_data.area,
132 txn->auth.method_data.data,
133 http_auth->area, global.tune.bufsize - 1);
134
135 if (len < 0)
136 return 0;
137
138
139 http_auth->area[len] = '\0';
140
141 p = strchr(http_auth->area, ':');
142
143 if (!p)
144 return 0;
145
146 txn->auth.user = http_auth->area;
147 *p = '\0';
148 txn->auth.pass = p+1;
149
150 txn->auth.method = HTTP_AUTH_BASIC;
151 return 1;
152 }
153
154 return 0;
155}
156
157/* This function ensures that the prerequisites for an L7 fetch are ready,
158 * which means that a request or response is ready. If some data is missing,
159 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Fauletef453ed2018-10-24 21:39:27 +0200160 * to extract data from L7.
161 *
162 * The function returns :
163 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
164 * decide whether or not an HTTP message is present ;
165 * NULL if the requested data cannot be fetched or if it is certain that
166 * we'll never have any HTTP message there ;
167 * The HTX message if ready
168 */
169struct htx *smp_prefetch_htx(struct sample *smp, const struct arg *args)
170{
171 struct proxy *px = smp->px;
172 struct stream *s = smp->strm;
173 unsigned int opt = smp->opt;
174 struct http_txn *txn = NULL;
175 struct htx *htx = NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100176 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200177
178 /* Note: it is possible that <s> is NULL when called before stream
179 * initialization (eg: tcp-request connection), so this function is the
180 * one responsible for guarding against this case for all HTTP users.
181 */
182 if (!s)
183 return NULL;
184
185 if (!s->txn) {
186 if (unlikely(!http_alloc_txn(s)))
187 return NULL; /* not enough memory */
188 http_init_txn(s);
189 txn = s->txn;
190 }
191
192 if (px->mode == PR_MODE_HTTP) {
193 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200194 htx = htx_from_buf(&s->req.buf);
195 if (htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH) {
196 /* Parsing is done by the mux, just wait */
197 smp->flags |= SMP_F_MAY_CHANGE;
198 return NULL;
199 }
200
201 /* OK we just got a valid HTTP request. We have some
202 * minor preparation to perform so that further checks
203 * can rely on HTTP tests.
204 */
205 if (txn) {
206 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100207 txn->meth = sl->info.req.meth;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200208 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
209 s->flags |= SF_REDIRECTABLE;
210 }
211
212 /* otherwise everything's ready for the request */
213 }
214 else {
215 htx = htx_from_buf(&s->res.buf);
216 if (htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH) {
217 /* Parsing is done by the mux, just wait */
218 smp->flags |= SMP_F_MAY_CHANGE;
219 return NULL;
220 }
221 }
222 }
223 else { /* PR_MODE_TCP */
224 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
225 struct buffer *buf;
226 struct h1m h1m;
227 struct http_hdr hdrs[MAX_HTTP_HDR];
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100228 union h1_sl h1sl;
229 unsigned int flags = HTX_FL_NONE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200230 int ret;
231
232 buf = &s->req.buf;
233 if (b_head(buf) + b_data(buf) > b_wrap(buf))
234 b_slow_realign(buf, trash.area, 0);
235
236 h1m_init_req(&h1m);
237 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100238 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200239 if (ret <= 0) {
240 /* Invalid or too big*/
241 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
242 return NULL;
243
244 /* wait for a full request */
245 smp->flags |= SMP_F_MAY_CHANGE;
246 return NULL;
247 }
248
249 /* OK we just got a valid HTTP request. We have to
250 * convert it into an HTX message.
251 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100252 if (unlikely(h1sl.rq.v.len == 0)) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200253 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100254 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200255 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100256 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200257 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100258 else if ((h1sl.rq.v.len == 8) &&
259 ((*(h1sl.rq.v.ptr + 5) > '1') ||
260 ((*(h1sl.rq.v.ptr + 5) == '1') && (*(h1sl.rq.v.ptr + 7) >= '1'))))
261 h1m.flags |= H1_MF_VER_11;
262
263
264 /* Set HTX start-line flags */
265 if (h1m.flags & H1_MF_VER_11)
266 flags |= HTX_SL_F_VER_11;
267 if (h1m.flags & H1_MF_XFER_ENC)
268 flags |= HTX_SL_F_XFER_ENC;
269 if (h1m.flags & H1_MF_XFER_LEN) {
270 flags |= HTX_SL_F_XFER_LEN;
271 if (h1m.flags & H1_MF_CHNK)
272 flags |= HTX_SL_F_CHNK;
273 else if (h1m.flags & H1_MF_CLEN)
274 flags |= HTX_SL_F_CLEN;
275 }
276
Christopher Fauletef453ed2018-10-24 21:39:27 +0200277 htx = htx_from_buf(get_trash_chunk());
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100278 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
279 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200280 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100281 sl->info.req.meth = h1sl.rq.meth;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200282
283 if (txn) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100284 txn->meth = h1sl.rq.meth;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200285 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
286 s->flags |= SF_REDIRECTABLE;
287 }
288 /* Ok, now everything's ready for the request */
289 }
290 else {
291 /* Impossible, no HTTP fetch on tcp-response */
292 return NULL;
293 }
294 }
295
296 /* everything's OK */
297 smp->data.u.sint = 1;
298 return htx;
299}
300
301/* This function ensures that the prerequisites for an L7 fetch are ready,
302 * which means that a request or response is ready. If some data is missing,
303 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau79e57332018-10-02 16:01:16 +0200304 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
305 * another test is made to ensure the required information is not gone.
306 *
307 * The function returns :
308 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
309 * decide whether or not an HTTP message is present ;
310 * 0 if the requested data cannot be fetched or if it is certain that
311 * we'll never have any HTTP message there ;
312 * 1 if an HTTP message is ready
313 */
314int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
315 const struct arg *args, struct sample *smp, int req_vol)
316{
317 struct http_txn *txn;
318 struct http_msg *msg;
319
320 /* Note: it is possible that <s> is NULL when called before stream
321 * initialization (eg: tcp-request connection), so this function is the
322 * one responsible for guarding against this case for all HTTP users.
323 */
324 if (!s)
325 return 0;
326
327 if (!s->txn) {
328 if (unlikely(!http_alloc_txn(s)))
329 return 0; /* not enough memory */
330 http_init_txn(s);
331 }
332 txn = s->txn;
333 msg = &txn->req;
334
335 /* Check for a dependency on a request */
336 smp->data.type = SMP_T_BOOL;
337
338 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
339 /* If the buffer does not leave enough free space at the end,
340 * we must first realign it.
341 */
342 if (ci_head(&s->req) > b_orig(&s->req.buf) &&
343 ci_head(&s->req) + ci_data(&s->req) > b_wrap(&s->req.buf) - global.tune.maxrewrite)
344 channel_slow_realign(&s->req, trash.area);
345
346 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
347 if (msg->msg_state == HTTP_MSG_ERROR)
348 return 0;
349
350 /* Try to decode HTTP request */
351 if (likely(msg->next < ci_data(&s->req)))
352 http_msg_analyzer(msg, &txn->hdr_idx);
353
354 /* Still no valid request ? */
355 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
356 if ((msg->msg_state == HTTP_MSG_ERROR) ||
357 channel_full(&s->req, global.tune.maxrewrite)) {
358 return 0;
359 }
360 /* wait for final state */
361 smp->flags |= SMP_F_MAY_CHANGE;
362 return 0;
363 }
364
365 /* OK we just got a valid HTTP request. We have some minor
366 * preparation to perform so that further checks can rely
367 * on HTTP tests.
368 */
369
370 /* If the request was parsed but was too large, we must absolutely
371 * return an error so that it is not processed. At the moment this
372 * cannot happen, but if the parsers are to change in the future,
373 * we want this check to be maintained.
374 */
375 if (unlikely(ci_head(&s->req) + ci_data(&s->req) >
376 b_wrap(&s->req.buf) - global.tune.maxrewrite)) {
377 msg->err_state = msg->msg_state;
378 msg->msg_state = HTTP_MSG_ERROR;
379 smp->data.u.sint = 1;
380 return 1;
381 }
382
383 txn->meth = find_http_meth(ci_head(msg->chn), msg->sl.rq.m_l);
384 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
385 s->flags |= SF_REDIRECTABLE;
386
387 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
388 return 0;
389 }
390
391 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
392 return 0; /* data might have moved and indexes changed */
393 }
394
395 /* otherwise everything's ready for the request */
396 }
397 else {
398 /* Check for a dependency on a response */
399 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
400 smp->flags |= SMP_F_MAY_CHANGE;
401 return 0;
402 }
403 }
404
405 /* everything's OK */
406 smp->data.u.sint = 1;
407 return 1;
408}
409
410/* This function fetches the method of current HTTP request and stores
411 * it in the global pattern struct as a chunk. There are two possibilities :
412 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
413 * in <len> and <ptr> is NULL ;
414 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
415 * <len> to its length.
416 * This is intended to be used with pat_match_meth() only.
417 */
418static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
419{
420 int meth;
421 struct http_txn *txn;
422
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200423 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
424 /* HTX version */
425 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +0200426
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200427 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +0200428 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200429
430 txn = smp->strm->txn;
431 meth = txn->meth;
432 smp->data.type = SMP_T_METH;
433 smp->data.u.meth.meth = meth;
434 if (meth == HTTP_METH_OTHER) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100435 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200436
437 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
438 /* ensure the indexes are not affected */
439 return 0;
440
441 sl = http_find_stline(htx);
442 smp->flags |= SMP_F_CONST;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100443 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
444 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200445 }
446 smp->flags |= SMP_F_VOL_1ST;
447 }
448 else {
449 /* LEGACY version */
450 CHECK_HTTP_MESSAGE_FIRST_PERM();
451
452 txn = smp->strm->txn;
453 meth = txn->meth;
454 smp->data.type = SMP_T_METH;
455 smp->data.u.meth.meth = meth;
456 if (meth == HTTP_METH_OTHER) {
457 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
458 /* ensure the indexes are not affected */
459 return 0;
460 smp->flags |= SMP_F_CONST;
461 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
462 smp->data.u.meth.str.area = ci_head(txn->req.chn);
463 }
464 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200465 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200466 return 1;
467}
468
469static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
470{
471 struct http_txn *txn;
472 char *ptr;
473 int len;
474
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200475 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
476 /* HTX version */
477 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100478 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200479
480 if (!htx)
481 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200482
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200483 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100484 len = HTX_SL_REQ_VLEN(sl);
485 ptr = HTX_SL_REQ_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200486 }
487 else {
488 /* LEGACY version */
489 CHECK_HTTP_MESSAGE_FIRST();
490
491 txn = smp->strm->txn;
492 len = txn->req.sl.rq.v_l;
493 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.v;
494 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200495
496 while ((len-- > 0) && (*ptr++ != '/'));
497 if (len <= 0)
498 return 0;
499
500 smp->data.type = SMP_T_STR;
501 smp->data.u.str.area = ptr;
502 smp->data.u.str.data = len;
503
504 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
505 return 1;
506}
507
508static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
509{
510 struct http_txn *txn;
511 char *ptr;
512 int len;
513
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200514 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
515 /* HTX version */
516 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100517 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200518
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200519 if (!htx)
520 return 0;
521
522 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100523 len = HTX_SL_RES_VLEN(sl);
524 ptr = HTX_SL_RES_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200525 }
526 else {
527 /* LEGACY version */
528 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +0200529
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200530 txn = smp->strm->txn;
531 if (txn->rsp.msg_state < HTTP_MSG_BODY)
532 return 0;
533
534 len = txn->rsp.sl.st.v_l;
535 ptr = ci_head(txn->rsp.chn);
536 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200537
538 while ((len-- > 0) && (*ptr++ != '/'));
539 if (len <= 0)
540 return 0;
541
542 smp->data.type = SMP_T_STR;
543 smp->data.u.str.area = ptr;
544 smp->data.u.str.data = len;
545
546 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
547 return 1;
548}
549
550/* 3. Check on Status Code. We manipulate integers here. */
551static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
552{
553 struct http_txn *txn;
554 char *ptr;
555 int len;
556
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200557 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
558 /* HTX version */
559 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100560 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200561
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200562 if (!htx)
563 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200564
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200565 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100566 len = HTX_SL_RES_CLEN(sl);
567 ptr = HTX_SL_RES_CPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200568 }
569 else {
570 /* LEGACY version */
571 CHECK_HTTP_MESSAGE_FIRST();
572
573 txn = smp->strm->txn;
574 if (txn->rsp.msg_state < HTTP_MSG_BODY)
575 return 0;
576
577 len = txn->rsp.sl.st.c_l;
578 ptr = ci_head(txn->rsp.chn) + txn->rsp.sl.st.c;
579 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200580
581 smp->data.type = SMP_T_SINT;
582 smp->data.u.sint = __strl2ui(ptr, len);
583 smp->flags = SMP_F_VOL_1ST;
584 return 1;
585}
586
587static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
588{
589 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
590 return 0;
591
592 if (!smp->strm->unique_id) {
593 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
594 return 0;
595 smp->strm->unique_id[0] = '\0';
596 }
597 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
598 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
599
600 smp->data.type = SMP_T_STR;
601 smp->data.u.str.area = smp->strm->unique_id;
602 smp->flags = SMP_F_CONST;
603 return 1;
604}
605
606/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800607 * empty line which separes headers from the body. This is useful
608 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200609 */
610static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
611{
Willy Tarreau79e57332018-10-02 16:01:16 +0200612 struct http_txn *txn;
613
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200614 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
615 /* HTX version */
616 struct htx *htx = smp_prefetch_htx(smp, args);
617 struct buffer *temp;
618 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200619
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200620 if (!htx)
621 return 0;
622 temp = get_trash_chunk();
623 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
624 struct htx_blk *blk = htx_get_blk(htx, pos);
625 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200626
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200627 if (type == HTX_BLK_HDR) {
628 struct ist n = htx_get_blk_name(htx, blk);
629 struct ist v = htx_get_blk_value(htx, blk);
630
631 if (!htx_hdr_to_str(n, v, temp))
632 return 0;
633 }
634 else if (type == HTX_BLK_EOH) {
635 if (!chunk_memcat(temp, "\r\n", 2))
636 return 0;
637 break;
638 }
639 }
640 smp->data.type = SMP_T_STR;
641 smp->data.u.str = *temp;
642
643 }
644 else {
645 /* LEGACY version */
646 struct http_msg *msg;
647 struct hdr_idx *idx;
648
649 CHECK_HTTP_MESSAGE_FIRST();
650
651 txn = smp->strm->txn;
652 idx = &txn->hdr_idx;
653 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200654
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200655 smp->data.type = SMP_T_STR;
656 smp->data.u.str.area = ci_head(msg->chn) + hdr_idx_first_pos(idx);
657 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
658 (ci_head(msg->chn)[msg->eoh] == '\r');
659 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200660 return 1;
661}
662
663/* Returns the header request in a length/value encoded format.
664 * This is useful for exchanges with the SPOE.
665 *
666 * A "length value" is a multibyte code encoding numbers. It uses the
667 * SPOE format. The encoding is the following:
668 *
669 * Each couple "header name" / "header value" is composed
670 * like this:
671 * "length value" "header name bytes"
672 * "length value" "header value bytes"
673 * When the last header is reached, the header name and the header
674 * value are empty. Their length are 0
675 */
676static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
677{
Willy Tarreau79e57332018-10-02 16:01:16 +0200678 struct http_txn *txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200679 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200680
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200681 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
682 /* HTX version */
683 struct htx *htx = smp_prefetch_htx(smp, args);
684 struct buffer *temp;
685 char *p, *end;
686 int32_t pos;
687 int ret;
Willy Tarreau79e57332018-10-02 16:01:16 +0200688
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200689 if (!htx)
690 return 0;
691 temp = get_trash_chunk();
692 p = temp->area;
693 end = temp->area + temp->size;
694 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
695 struct htx_blk *blk = htx_get_blk(htx, pos);
696 enum htx_blk_type type = htx_get_blk_type(blk);
697 struct ist n, v;
Willy Tarreau79e57332018-10-02 16:01:16 +0200698
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200699 if (type == HTX_BLK_HDR) {
700 n = htx_get_blk_name(htx,blk);
701 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200702
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200703 /* encode the header name. */
704 ret = encode_varint(n.len, &p, end);
705 if (ret == -1)
706 return 0;
707 if (p + n.len > end)
708 return 0;
709 memcpy(p, n.ptr, n.len);
710 p += n.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200711
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200712 /* encode the header value. */
713 ret = encode_varint(v.len, &p, end);
714 if (ret == -1)
715 return 0;
716 if (p + v.len > end)
717 return 0;
718 memcpy(p, v.ptr, v.len);
719 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200720
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200721 }
722 else if (type == HTX_BLK_EOH) {
723 /* encode the end of the header list with empty
724 * header name and header value.
725 */
726 ret = encode_varint(0, &p, end);
727 if (ret == -1)
728 return 0;
729 ret = encode_varint(0, &p, end);
730 if (ret == -1)
731 return 0;
732 break;
733 }
734 }
735
736 /* Initialise sample data which will be filled. */
737 smp->data.type = SMP_T_BIN;
738 smp->data.u.str.area = temp->area;
739 smp->data.u.str.data = p - temp->area;
740 smp->data.u.str.size = temp->size;
741 }
742 else {
743 /* LEGACY version */
744 struct http_msg *msg;
745 struct hdr_idx *idx;
746 const char *cur_ptr, *cur_next, *p;
747 int old_idx, cur_idx;
748 struct hdr_idx_elem *cur_hdr;
749 const char *hn, *hv;
750 int hnl, hvl;
751 int ret;
752 char *buf;
753 char *end;
754
755 CHECK_HTTP_MESSAGE_FIRST();
756
757 temp = get_trash_chunk();
758 buf = temp->area;
759 end = temp->area + temp->size;
760
761 txn = smp->strm->txn;
762 idx = &txn->hdr_idx;
763 msg = &txn->req;
764
765 /* Build array of headers. */
766 old_idx = 0;
767 cur_next = ci_head(msg->chn) + hdr_idx_first_pos(idx);
768 while (1) {
769 cur_idx = idx->v[old_idx].next;
770 if (!cur_idx)
771 break;
772 old_idx = cur_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +0200773
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200774 cur_hdr = &idx->v[cur_idx];
775 cur_ptr = cur_next;
776 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
777
778 /* Now we have one full header at cur_ptr of len cur_hdr->len,
779 * and the next header starts at cur_next. We'll check
780 * this header in the list as well as against the default
781 * rule.
782 */
783
784 /* look for ': *'. */
785 hn = cur_ptr;
786 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
787 if (p >= cur_ptr+cur_hdr->len)
788 continue;
789 hnl = p - hn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200790 p++;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200791 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
792 p++;
793 if (p >= cur_ptr + cur_hdr->len)
794 continue;
795 hv = p;
796 hvl = cur_ptr + cur_hdr->len-p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200797
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200798 /* encode the header name. */
799 ret = encode_varint(hnl, &buf, end);
800 if (ret == -1)
801 return 0;
802 if (buf + hnl > end)
803 return 0;
804 memcpy(buf, hn, hnl);
805 buf += hnl;
806
807 /* encode and copy the value. */
808 ret = encode_varint(hvl, &buf, end);
809 if (ret == -1)
810 return 0;
811 if (buf + hvl > end)
812 return 0;
813 memcpy(buf, hv, hvl);
814 buf += hvl;
815 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200816
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200817 /* encode the end of the header list with empty
818 * header name and header value.
819 */
820 ret = encode_varint(0, &buf, end);
Willy Tarreau79e57332018-10-02 16:01:16 +0200821 if (ret == -1)
822 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200823 ret = encode_varint(0, &buf, end);
824 if (ret == -1)
Willy Tarreau79e57332018-10-02 16:01:16 +0200825 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200826
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200827 /* Initialise sample data which will be filled. */
828 smp->data.type = SMP_T_BIN;
829 smp->data.u.str.area = temp->area;
830 smp->data.u.str.data = buf - temp->area;
831 smp->data.u.str.size = temp->size;
832 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200833 return 1;
834}
835
836/* returns the longest available part of the body. This requires that the body
837 * has been waited for using http-buffer-request.
838 */
839static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
840{
Willy Tarreau79e57332018-10-02 16:01:16 +0200841 struct buffer *temp;
842
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200843 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
844 /* HTX version */
845 struct htx *htx = smp_prefetch_htx(smp, args);
846 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200847
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200848 if (!htx)
849 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200850
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200851 temp = get_trash_chunk();
852 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
853 struct htx_blk *blk = htx_get_blk(htx, pos);
854 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200855
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200856 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
857 break;
858 if (type == HTX_BLK_DATA) {
859 if (!htx_data_to_str(htx_get_blk_value(htx, blk), temp, 0))
860 return 0;
861 }
862 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200863
Willy Tarreau79e57332018-10-02 16:01:16 +0200864 smp->data.type = SMP_T_BIN;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200865 smp->data.u.str = *temp;
866 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200867 }
868 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200869 /* LEGACY version */
870 struct http_msg *msg;
871 unsigned long len;
872 unsigned long block1;
873 char *body;
874
875 CHECK_HTTP_MESSAGE_FIRST();
876
877 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
878 msg = &smp->strm->txn->req;
879 else
880 msg = &smp->strm->txn->rsp;
881
882 len = http_body_bytes(msg);
883 body = c_ptr(msg->chn, -http_data_rewind(msg));
884
885 block1 = len;
886 if (block1 > b_wrap(&msg->chn->buf) - body)
887 block1 = b_wrap(&msg->chn->buf) - body;
888
889 if (block1 == len) {
890 /* buffer is not wrapped (or empty) */
891 smp->data.type = SMP_T_BIN;
892 smp->data.u.str.area = body;
893 smp->data.u.str.data = len;
894 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
895 }
896 else {
897 /* buffer is wrapped, we need to defragment it */
898 temp = get_trash_chunk();
899 memcpy(temp->area, body, block1);
900 memcpy(temp->area + block1, b_orig(&msg->chn->buf),
901 len - block1);
902 smp->data.type = SMP_T_BIN;
903 smp->data.u.str.area = temp->area;
904 smp->data.u.str.data = len;
905 smp->flags = SMP_F_VOL_TEST;
906 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200907 }
908 return 1;
909}
910
911
912/* returns the available length of the body. This requires that the body
913 * has been waited for using http-buffer-request.
914 */
915static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
916{
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200917 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
918 /* HTX version */
919 return 0; /* TODO: to be implemented */
920 }
921 else {
922 /* LEGACY version */
923 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200924
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200925 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +0200926
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200927 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
928 msg = &smp->strm->txn->req;
929 else
930 msg = &smp->strm->txn->rsp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200931
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200932 smp->data.type = SMP_T_SINT;
933 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreau79e57332018-10-02 16:01:16 +0200934
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200935 smp->flags = SMP_F_VOL_TEST;
936 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200937 return 1;
938}
939
940
941/* returns the advertised length of the body, or the advertised size of the
942 * chunks available in the buffer. This requires that the body has been waited
943 * for using http-buffer-request.
944 */
945static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
946{
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200947 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
948 /* HTX version */
949 return 0; /* TODO: to be implemented */
950 }
951 else {
952 /* LEGACY version */
953 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200954
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200955 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +0200956
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200957 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
958 msg = &smp->strm->txn->req;
959 else
960 msg = &smp->strm->txn->rsp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200961
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200962 smp->data.type = SMP_T_SINT;
963 smp->data.u.sint = msg->body_len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200964
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200965 smp->flags = SMP_F_VOL_TEST;
966 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200967 return 1;
968}
969
970
971/* 4. Check on URL/URI. A pointer to the URI is stored. */
972static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
973{
974 struct http_txn *txn;
975
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200976 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
977 /* HTX version */
978 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100979 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200980
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200981 if (!htx)
982 return 0;
983 sl = http_find_stline(htx);
984 smp->data.type = SMP_T_STR;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100985 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
986 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200987 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
988 }
989 else {
990 /* LEGACY version */
991 CHECK_HTTP_MESSAGE_FIRST();
992
993 txn = smp->strm->txn;
994 smp->data.type = SMP_T_STR;
995 smp->data.u.str.data = txn->req.sl.rq.u_l;
996 smp->data.u.str.area = ci_head(txn->req.chn) + txn->req.sl.rq.u;
997 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
998 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200999 return 1;
1000}
1001
1002static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1003{
1004 struct http_txn *txn;
1005 struct sockaddr_storage addr;
1006
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001007 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1008 /* HTX version */
1009 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001010 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001011
1012 if (!htx)
1013 return 0;
1014 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001015 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001016 }
1017 else {
1018 /* LEGACY version */
1019 CHECK_HTTP_MESSAGE_FIRST();
1020
1021 txn = smp->strm->txn;
1022 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
1023 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001024
Willy Tarreau79e57332018-10-02 16:01:16 +02001025 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1026 return 0;
1027
1028 smp->data.type = SMP_T_IPV4;
1029 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
1030 smp->flags = 0;
1031 return 1;
1032}
1033
1034static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
1035{
1036 struct http_txn *txn;
1037 struct sockaddr_storage addr;
1038
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001039 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1040 /* HTX version */
1041 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001042 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001043
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001044 if (!htx)
1045 return 0;
1046 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001047 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001048 }
1049 else {
1050 /* LEGACY version */
1051 CHECK_HTTP_MESSAGE_FIRST();
1052
1053 txn = smp->strm->txn;
1054 url2sa(ci_head(txn->req.chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
1055 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001056 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1057 return 0;
1058
1059 smp->data.type = SMP_T_SINT;
1060 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
1061 smp->flags = 0;
1062 return 1;
1063}
1064
1065/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1066 * Accepts an optional argument of type string containing the header field name,
1067 * and an optional argument of type signed or unsigned integer to request an
1068 * explicit occurrence of the header. Note that in the event of a missing name,
1069 * headers are considered from the first one. It does not stop on commas and
1070 * returns full lines instead (useful for User-Agent or Date for example).
1071 */
1072static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1073{
Willy Tarreau79e57332018-10-02 16:01:16 +02001074 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001075
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001076 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1077 /* HTX version */
1078 struct htx *htx = smp_prefetch_htx(smp, args);
1079 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1080 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +02001081
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001082 if (!ctx) {
1083 /* first call */
1084 ctx = &static_http_hdr_ctx;
1085 ctx->blk = NULL;
1086 smp->ctx.a[0] = ctx;
1087 }
1088
1089 if (args) {
1090 if (args[0].type != ARGT_STR)
1091 return 0;
1092 name.ptr = args[0].data.str.area;
1093 name.len = args[0].data.str.data;
1094
1095 if (args[1].type == ARGT_SINT)
1096 occ = args[1].data.sint;
1097 }
1098
1099 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001100 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001101
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001102 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1103 /* search for header from the beginning */
1104 ctx->blk = NULL;
1105
1106 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1107 /* no explicit occurrence and single fetch => last header by default */
1108 occ = -1;
1109
1110 if (!occ)
1111 /* prepare to report multiple occurrences for ACL fetches */
1112 smp->flags |= SMP_F_NOT_LAST;
1113
1114 smp->data.type = SMP_T_STR;
1115 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1116 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1117 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001118 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001119 else {
1120 /* LEGACY version */
1121 struct hdr_idx *idx;
1122 struct hdr_ctx *ctx = smp->ctx.a[0];
1123 const struct http_msg *msg;
1124 const char *name_str = NULL;
1125 int name_len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001126
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001127 if (!ctx) {
1128 /* first call */
1129 ctx = &static_hdr_ctx;
1130 ctx->idx = 0;
1131 smp->ctx.a[0] = ctx;
1132 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001133
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001134 if (args) {
1135 if (args[0].type != ARGT_STR)
1136 return 0;
1137 name_str = args[0].data.str.area;
1138 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001139
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001140 if (args[1].type == ARGT_SINT)
1141 occ = args[1].data.sint;
1142 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001143
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001144 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001145
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001146 idx = &smp->strm->txn->hdr_idx;
1147 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 +02001148
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001149 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1150 /* search for header from the beginning */
1151 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001152
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001153 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1154 /* no explicit occurrence and single fetch => last header by default */
1155 occ = -1;
1156
1157 if (!occ)
1158 /* prepare to report multiple occurrences for ACL fetches */
1159 smp->flags |= SMP_F_NOT_LAST;
1160
1161 smp->data.type = SMP_T_STR;
1162 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1163 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1164 return 1;
1165 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001166 smp->flags &= ~SMP_F_NOT_LAST;
1167 return 0;
1168}
1169
1170/* 6. Check on HTTP header count. The number of occurrences is returned.
1171 * Accepts exactly 1 argument of type string. It does not stop on commas and
1172 * returns full lines instead (useful for User-Agent or Date for example).
1173 */
1174static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1175{
Willy Tarreau79e57332018-10-02 16:01:16 +02001176 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001177
1178 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1179 /* HTX version */
1180 struct htx *htx = smp_prefetch_htx(smp, args);
1181 struct http_hdr_ctx ctx;
1182 struct ist name;
1183
1184 if (!htx)
1185 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001186
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001187 if (args && args->type == ARGT_STR) {
1188 name.ptr = args->data.str.area;
1189 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001190 } else {
1191 name.ptr = NULL;
1192 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001193 }
1194
1195 ctx.blk = NULL;
1196 cnt = 0;
1197 while (http_find_header(htx, name, &ctx, 1))
1198 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001199 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001200 else {
1201 /* LEGACY version */
1202 struct hdr_idx *idx;
1203 struct hdr_ctx ctx;
1204 const struct http_msg *msg;
1205 const char *name = NULL;
1206 int len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001207
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001208 if (args && args->type == ARGT_STR) {
1209 name = args->data.str.area;
1210 len = args->data.str.data;
1211 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001212
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001213 CHECK_HTTP_MESSAGE_FIRST();
1214
1215 idx = &smp->strm->txn->hdr_idx;
1216 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 +02001217
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001218 ctx.idx = 0;
1219 cnt = 0;
1220 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
1221 cnt++;
1222 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001223
1224 smp->data.type = SMP_T_SINT;
1225 smp->data.u.sint = cnt;
1226 smp->flags = SMP_F_VOL_HDR;
1227 return 1;
1228}
1229
1230static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
1231{
Willy Tarreau79e57332018-10-02 16:01:16 +02001232 struct buffer *temp;
1233 char del = ',';
1234
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001235 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1236 /* HTX version */
1237 struct htx *htx = smp_prefetch_htx(smp, args);
1238 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001239
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001240 if (!htx)
1241 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001242
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001243 if (args && args->type == ARGT_STR)
1244 del = *args[0].data.str.area;
Willy Tarreau79e57332018-10-02 16:01:16 +02001245
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001246 temp = get_trash_chunk();
1247 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1248 struct htx_blk *blk = htx_get_blk(htx, pos);
1249 enum htx_blk_type type = htx_get_blk_type(blk);
1250 struct ist n;
Willy Tarreau79e57332018-10-02 16:01:16 +02001251
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001252 if (type == HTX_BLK_EOH)
1253 break;
1254 if (type != HTX_BLK_HDR)
1255 continue;
1256 n = htx_get_blk_name(htx, blk);
1257
1258 if (temp->data)
1259 temp->area[temp->data++] = del;
1260 chunk_memcat(temp, n.ptr, n.len);
1261 }
1262 }
1263 else {
1264 /* LEGACY version */
1265 struct hdr_idx *idx;
1266 struct hdr_ctx ctx;
1267 const struct http_msg *msg;
1268
1269 if (args && args->type == ARGT_STR)
1270 del = *args[0].data.str.area;
1271
1272 CHECK_HTTP_MESSAGE_FIRST();
1273
1274 idx = &smp->strm->txn->hdr_idx;
1275 msg = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &smp->strm->txn->req : &smp->strm->txn->rsp;
1276
1277 temp = get_trash_chunk();
1278
1279 ctx.idx = 0;
1280 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
1281 if (temp->data)
1282 temp->area[temp->data++] = del;
1283 memcpy(temp->area + temp->data, ctx.line, ctx.del);
1284 temp->data += ctx.del;
1285 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001286 }
1287
1288 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001289 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001290 smp->flags = SMP_F_VOL_HDR;
1291 return 1;
1292}
1293
1294/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1295 * Accepts an optional argument of type string containing the header field name,
1296 * and an optional argument of type signed or unsigned integer to request an
1297 * explicit occurrence of the header. Note that in the event of a missing name,
1298 * headers are considered from the first one.
1299 */
1300static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1301{
Willy Tarreau79e57332018-10-02 16:01:16 +02001302 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001303
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001304 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1305 /* HTX version */
1306 struct htx *htx = smp_prefetch_htx(smp, args);
1307 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1308 struct ist name;
1309
1310 if (!ctx) {
1311 /* first call */
1312 ctx = &static_http_hdr_ctx;
1313 ctx->blk = NULL;
1314 smp->ctx.a[0] = ctx;
1315 }
1316
1317 if (args) {
1318 if (args[0].type != ARGT_STR)
1319 return 0;
1320 name.ptr = args[0].data.str.area;
1321 name.len = args[0].data.str.data;
1322
1323 if (args[1].type == ARGT_SINT)
1324 occ = args[1].data.sint;
1325 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001326
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001327 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001328 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001329
1330 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1331 /* search for header from the beginning */
1332 ctx->blk = NULL;
1333
1334 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1335 /* no explicit occurrence and single fetch => last header by default */
1336 occ = -1;
1337
1338 if (!occ)
1339 /* prepare to report multiple occurrences for ACL fetches */
1340 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001341
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001342 smp->data.type = SMP_T_STR;
1343 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1344 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1345 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001346 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001347 else {
1348 /* LEGACY version */
1349 struct hdr_idx *idx;
1350 struct hdr_ctx *ctx = smp->ctx.a[0];
1351 const struct http_msg *msg;
1352 const char *name_str = NULL;
1353 int name_len = 0;
1354
1355 if (!ctx) {
1356 /* first call */
1357 ctx = &static_hdr_ctx;
1358 ctx->idx = 0;
1359 smp->ctx.a[0] = ctx;
1360 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001361
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001362 if (args) {
1363 if (args[0].type != ARGT_STR)
1364 return 0;
1365 name_str = args[0].data.str.area;
1366 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001367
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001368 if (args[1].type == ARGT_SINT)
1369 occ = args[1].data.sint;
1370 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001371
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001372 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001373
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001374 idx = &smp->strm->txn->hdr_idx;
1375 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 +02001376
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001377 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1378 /* search for header from the beginning */
1379 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001380
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001381 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1382 /* no explicit occurrence and single fetch => last header by default */
1383 occ = -1;
1384
1385 if (!occ)
1386 /* prepare to report multiple occurrences for ACL fetches */
1387 smp->flags |= SMP_F_NOT_LAST;
1388
1389 smp->data.type = SMP_T_STR;
1390 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1391 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1392 return 1;
1393 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001394
1395 smp->flags &= ~SMP_F_NOT_LAST;
1396 return 0;
1397}
1398
1399/* 6. Check on HTTP header count. The number of occurrences is returned.
1400 * Accepts exactly 1 argument of type string.
1401 */
1402static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1403{
Willy Tarreau79e57332018-10-02 16:01:16 +02001404 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001405
1406 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1407 /* HTX version */
1408 struct htx *htx = smp_prefetch_htx(smp, args);
1409 struct http_hdr_ctx ctx;
1410 struct ist name;
1411
1412 if (!htx)
1413 return 0;
1414
1415 if (args && args->type == ARGT_STR) {
1416 name.ptr = args->data.str.area;
1417 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001418 } else {
1419 name.ptr = NULL;
1420 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001421 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001422
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001423 ctx.blk = NULL;
1424 cnt = 0;
1425 while (http_find_header(htx, name, &ctx, 0))
1426 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001427 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001428 else {
1429 /* LEGACY version */
1430 struct hdr_idx *idx;
1431 struct hdr_ctx ctx;
1432 const struct http_msg *msg;
1433 const char *name = NULL;
1434 int len = 0;
1435
1436 if (args && args->type == ARGT_STR) {
1437 name = args->data.str.area;
1438 len = args->data.str.data;
1439 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001440
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001441 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001442
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001443 idx = &smp->strm->txn->hdr_idx;
1444 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 +02001445
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001446 ctx.idx = 0;
1447 cnt = 0;
1448 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
1449 cnt++;
1450 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001451
1452 smp->data.type = SMP_T_SINT;
1453 smp->data.u.sint = cnt;
1454 smp->flags = SMP_F_VOL_HDR;
1455 return 1;
1456}
1457
1458/* Fetch an HTTP header's integer value. The integer value is returned. It
1459 * takes a mandatory argument of type string and an optional one of type int
1460 * to designate a specific occurrence. It returns an unsigned integer, which
1461 * may or may not be appropriate for everything.
1462 */
1463static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1464{
1465 int ret = smp_fetch_hdr(args, smp, kw, private);
1466
1467 if (ret > 0) {
1468 smp->data.type = SMP_T_SINT;
1469 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1470 smp->data.u.str.data);
1471 }
1472
1473 return ret;
1474}
1475
1476/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1477 * and an optional one of type int to designate a specific occurrence.
1478 * It returns an IPv4 or IPv6 address.
1479 */
1480static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1481{
1482 int ret;
1483
1484 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
1485 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
1486 smp->data.type = SMP_T_IPV4;
1487 break;
1488 } else {
1489 struct buffer *temp = get_trash_chunk();
1490 if (smp->data.u.str.data < temp->size - 1) {
1491 memcpy(temp->area, smp->data.u.str.area,
1492 smp->data.u.str.data);
1493 temp->area[smp->data.u.str.data] = '\0';
1494 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1495 smp->data.type = SMP_T_IPV6;
1496 break;
1497 }
1498 }
1499 }
1500
1501 /* if the header doesn't match an IP address, fetch next one */
1502 if (!(smp->flags & SMP_F_NOT_LAST))
1503 return 0;
1504 }
1505 return ret;
1506}
1507
1508/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1509 * the first '/' after the possible hostname, and ends before the possible '?'.
1510 */
1511static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1512{
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001513 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1514 /* HTX version */
1515 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001516 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001517 struct ist path;
1518 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001519
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001520 if (!htx)
1521 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001522
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001523 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001524 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001525 if (!path.ptr)
1526 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001527
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001528 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Willy Tarreau79e57332018-10-02 16:01:16 +02001529
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001530 /* OK, we got the '/' ! */
1531 smp->data.type = SMP_T_STR;
1532 smp->data.u.str.area = path.ptr;
1533 smp->data.u.str.data = len;
1534 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1535 }
1536 else {
1537 struct http_txn *txn;
1538 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001539
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001540 CHECK_HTTP_MESSAGE_FIRST();
1541
1542 txn = smp->strm->txn;
1543 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1544 ptr = http_txn_get_path(txn);
1545 if (!ptr)
1546 return 0;
1547
1548 /* OK, we got the '/' ! */
1549 smp->data.type = SMP_T_STR;
1550 smp->data.u.str.area = ptr;
1551
1552 while (ptr < end && *ptr != '?')
1553 ptr++;
1554
1555 smp->data.u.str.data = ptr - smp->data.u.str.area;
1556 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1557 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001558 return 1;
1559}
1560
1561/* This produces a concatenation of the first occurrence of the Host header
1562 * followed by the path component if it begins with a slash ('/'). This means
1563 * that '*' will not be added, resulting in exactly the first Host entry.
1564 * If no Host header is found, then the path is returned as-is. The returned
1565 * value is stored in the trash so it does not need to be marked constant.
1566 * The returned sample is of type string.
1567 */
1568static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1569{
Willy Tarreau79e57332018-10-02 16:01:16 +02001570 struct buffer *temp;
1571
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001572 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1573 /* HTX version */
1574 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001575 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001576 struct http_hdr_ctx ctx;
1577 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001578
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001579 if (!htx)
1580 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001581
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001582 ctx.blk = NULL;
1583 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1584 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001585
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001586 /* OK we have the header value in ctx.value */
1587 temp = get_trash_chunk();
1588 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1589
1590 /* now retrieve the path */
1591 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001592 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001593 if (path.ptr) {
1594 size_t len;
1595
1596 for (len = 0; len < path.len && *(path.ptr) != '?'; len++);
1597 if (len && *(path.ptr) == '/')
1598 chunk_memcat(temp, path.ptr, len);
1599 }
1600
1601 smp->data.type = SMP_T_STR;
1602 smp->data.u.str = *temp;
1603 }
1604 else {
1605 /* LEGACY version */
1606 struct http_txn *txn;
1607 char *ptr, *end, *beg;
1608 struct hdr_ctx ctx;
1609
1610 CHECK_HTTP_MESSAGE_FIRST();
1611
1612 txn = smp->strm->txn;
1613 ctx.idx = 0;
1614 if (!http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
1615 return smp_fetch_path(args, smp, kw, private);
1616
1617 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1618 temp = get_trash_chunk();
1619 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1620 smp->data.type = SMP_T_STR;
1621 smp->data.u.str.area = temp->area;
1622 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001623
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001624 /* now retrieve the path */
1625 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1626 beg = http_txn_get_path(txn);
1627 if (!beg)
1628 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001629
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001630 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1631
1632 if (beg < ptr && *beg == '/') {
1633 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1634 ptr - beg);
1635 smp->data.u.str.data += ptr - beg;
1636 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001637 }
1638
1639 smp->flags = SMP_F_VOL_1ST;
1640 return 1;
1641}
1642
1643/* This produces a 32-bit hash of the concatenation of the first occurrence of
1644 * the Host header followed by the path component if it begins with a slash ('/').
1645 * This means that '*' will not be added, resulting in exactly the first Host
1646 * entry. If no Host header is found, then the path is used. The resulting value
1647 * is hashed using the path hash followed by a full avalanche hash and provides a
1648 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1649 * high-traffic sites without having to store whole paths.
1650 */
1651static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1652{
Willy Tarreau79e57332018-10-02 16:01:16 +02001653 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001654
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001655 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1656 /* HTX version */
1657 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001658 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001659 struct http_hdr_ctx ctx;
1660 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001661
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001662 if (!htx)
1663 return 0;
1664
1665 ctx.blk = NULL;
1666 if (!http_find_header(htx, ist("Host"), &ctx, 0)) {
1667 /* OK we have the header value in ctx.value */
1668 while (ctx.value.len--)
1669 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1670 }
1671
1672 /* now retrieve the path */
1673 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001674 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001675 if (path.ptr) {
1676 size_t len;
1677
1678 for (len = 0; len < path.len && *(path.ptr) != '?'; len++);
1679 if (len && *(path.ptr) == '/') {
1680 while (len--)
1681 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1682 }
1683 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001684 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001685 else {
1686 /* LEGACY version */
1687 struct http_txn *txn;
1688 struct hdr_ctx ctx;
1689 char *ptr, *beg, *end;
1690 int len;
1691
1692 CHECK_HTTP_MESSAGE_FIRST();
1693
1694 txn = smp->strm->txn;
1695 ctx.idx = 0;
1696 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
1697 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1698 ptr = ctx.line + ctx.val;
1699 len = ctx.vlen;
1700 while (len--)
1701 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1702 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001703
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001704 /* now retrieve the path */
1705 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
1706 beg = http_txn_get_path(txn);
1707 if (!beg)
1708 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001709
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001710 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001711
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001712 if (beg < ptr && *beg == '/') {
1713 while (beg < ptr)
1714 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
1715 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001716 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001717
Willy Tarreau79e57332018-10-02 16:01:16 +02001718 hash = full_hash(hash);
1719
1720 smp->data.type = SMP_T_SINT;
1721 smp->data.u.sint = hash;
1722 smp->flags = SMP_F_VOL_1ST;
1723 return 1;
1724}
1725
1726/* This concatenates the source address with the 32-bit hash of the Host and
1727 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1728 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1729 * on the source address length. The path hash is stored before the address so
1730 * that in environments where IPv6 is insignificant, truncating the output to
1731 * 8 bytes would still work.
1732 */
1733static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1734{
1735 struct buffer *temp;
1736 struct connection *cli_conn = objt_conn(smp->sess->origin);
1737
1738 if (!cli_conn)
1739 return 0;
1740
1741 if (!smp_fetch_base32(args, smp, kw, private))
1742 return 0;
1743
1744 temp = get_trash_chunk();
1745 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1746 temp->data += sizeof(unsigned int);
1747
1748 switch (cli_conn->addr.from.ss_family) {
1749 case AF_INET:
1750 memcpy(temp->area + temp->data,
1751 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1752 4);
1753 temp->data += 4;
1754 break;
1755 case AF_INET6:
1756 memcpy(temp->area + temp->data,
1757 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1758 16);
1759 temp->data += 16;
1760 break;
1761 default:
1762 return 0;
1763 }
1764
1765 smp->data.u.str = *temp;
1766 smp->data.type = SMP_T_BIN;
1767 return 1;
1768}
1769
1770/* Extracts the query string, which comes after the question mark '?'. If no
1771 * question mark is found, nothing is returned. Otherwise it returns a sample
1772 * of type string carrying the whole query string.
1773 */
1774static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1775{
Willy Tarreau79e57332018-10-02 16:01:16 +02001776 char *ptr, *end;
1777
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001778 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1779 /* HTX version */
1780 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001781 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001782
1783 if (!htx)
1784 return 0;
1785
1786 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001787 ptr = HTX_SL_REQ_UPTR(sl);
1788 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001789 }
1790 else {
1791 /* LEGACY version */
1792 struct http_txn *txn;
1793
1794 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02001795
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001796 txn = smp->strm->txn;
1797 ptr = ci_head(txn->req.chn) + txn->req.sl.rq.u;
1798 end = ptr + txn->req.sl.rq.u_l;
1799 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001800
1801 /* look up the '?' */
1802 do {
1803 if (ptr == end)
1804 return 0;
1805 } while (*ptr++ != '?');
1806
1807 smp->data.type = SMP_T_STR;
1808 smp->data.u.str.area = ptr;
1809 smp->data.u.str.data = end - ptr;
1810 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1811 return 1;
1812}
1813
1814static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1815{
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001816 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1817 /* HTX version */
1818 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +02001819
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001820 if (!htx)
1821 return 0;
1822 }
1823 else {
1824 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02001825
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001826 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
1827 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
1828 */
1829 CHECK_HTTP_MESSAGE_FIRST_PERM();
1830 }
1831 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001832 smp->data.u.sint = 1;
1833 return 1;
1834}
1835
1836/* return a valid test if the current request is the first one on the connection */
1837static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1838{
1839 smp->data.type = SMP_T_BOOL;
1840 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1841 return 1;
1842}
1843
1844/* Accepts exactly 1 argument of type userlist */
1845static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1846{
1847
1848 if (!args || args->type != ARGT_USR)
1849 return 0;
1850
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001851 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1852 /* HTX version */
1853 struct htx *htx = smp_prefetch_htx(smp, args);
Willy Tarreau79e57332018-10-02 16:01:16 +02001854
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001855 if (!htx)
1856 return 0;
1857 }
1858 else {
1859 /* LEGACY version */
1860 CHECK_HTTP_MESSAGE_FIRST();
1861 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001862
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001863 if (!get_http_auth(smp))
1864 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001865 smp->data.type = SMP_T_BOOL;
1866 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001867 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001868 return 1;
1869}
1870
1871/* Accepts exactly 1 argument of type userlist */
1872static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1873{
1874 if (!args || args->type != ARGT_USR)
1875 return 0;
1876
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001877 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
1878 /* HTX version */
1879 struct htx *htx = smp_prefetch_htx(smp, args);
1880
1881 if (!htx)
1882 return 0;
1883 }
1884 else {
1885 /* LEGACY version */
1886 CHECK_HTTP_MESSAGE_FIRST();
1887 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001888
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001889 if (!get_http_auth(smp))
Willy Tarreau79e57332018-10-02 16:01:16 +02001890 return 0;
1891
1892 /* if the user does not belong to the userlist or has a wrong password,
1893 * report that it unconditionally does not match. Otherwise we return
1894 * a string containing the username.
1895 */
1896 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1897 smp->strm->txn->auth.pass))
1898 return 0;
1899
1900 /* pat_match_auth() will need the user list */
1901 smp->ctx.a[0] = args->data.usr;
1902
1903 smp->data.type = SMP_T_STR;
1904 smp->flags = SMP_F_CONST;
1905 smp->data.u.str.area = smp->strm->txn->auth.user;
1906 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1907
1908 return 1;
1909}
1910
1911/* Fetch a captured HTTP request header. The index is the position of
1912 * the "capture" option in the configuration file
1913 */
1914static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1915{
1916 struct proxy *fe = strm_fe(smp->strm);
1917 int idx;
1918
1919 if (!args || args->type != ARGT_SINT)
1920 return 0;
1921
1922 idx = args->data.sint;
1923
1924 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1925 return 0;
1926
1927 smp->data.type = SMP_T_STR;
1928 smp->flags |= SMP_F_CONST;
1929 smp->data.u.str.area = smp->strm->req_cap[idx];
1930 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1931
1932 return 1;
1933}
1934
1935/* Fetch a captured HTTP response header. The index is the position of
1936 * the "capture" option in the configuration file
1937 */
1938static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1939{
1940 struct proxy *fe = strm_fe(smp->strm);
1941 int idx;
1942
1943 if (!args || args->type != ARGT_SINT)
1944 return 0;
1945
1946 idx = args->data.sint;
1947
1948 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1949 return 0;
1950
1951 smp->data.type = SMP_T_STR;
1952 smp->flags |= SMP_F_CONST;
1953 smp->data.u.str.area = smp->strm->res_cap[idx];
1954 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1955
1956 return 1;
1957}
1958
1959/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1960static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1961{
1962 struct buffer *temp;
1963 struct http_txn *txn = smp->strm->txn;
1964 char *ptr;
1965
1966 if (!txn || !txn->uri)
1967 return 0;
1968
1969 ptr = txn->uri;
1970
1971 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1972 ptr++;
1973
1974 temp = get_trash_chunk();
1975 temp->area = txn->uri;
1976 temp->data = ptr - txn->uri;
1977 smp->data.u.str = *temp;
1978 smp->data.type = SMP_T_STR;
1979 smp->flags = SMP_F_CONST;
1980
1981 return 1;
1982
1983}
1984
1985/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1986static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1987{
1988 struct http_txn *txn = smp->strm->txn;
1989 struct ist path;
1990 const char *ptr;
1991
1992 if (!txn || !txn->uri)
1993 return 0;
1994
1995 ptr = txn->uri;
1996
1997 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1998 ptr++;
1999
2000 if (!*ptr)
2001 return 0;
2002
Christopher Faulet78337bb2018-11-15 14:35:18 +01002003 /* skip the first space and find space after URI */
2004 path = ist2(++ptr, 0);
2005 while (*ptr != ' ' && *ptr != '\0')
2006 ptr++;
2007 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002008
Christopher Faulet78337bb2018-11-15 14:35:18 +01002009 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02002010 if (!path.ptr)
2011 return 0;
2012
2013 smp->data.u.str.area = path.ptr;
2014 smp->data.u.str.data = path.len;
2015 smp->data.type = SMP_T_STR;
2016 smp->flags = SMP_F_CONST;
2017
2018 return 1;
2019}
2020
2021/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
2022 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2023 */
2024static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2025{
2026 struct http_txn *txn = smp->strm->txn;
2027
2028 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2029 return 0;
2030
2031 if (txn->req.flags & HTTP_MSGF_VER_11)
2032 smp->data.u.str.area = "HTTP/1.1";
2033 else
2034 smp->data.u.str.area = "HTTP/1.0";
2035
2036 smp->data.u.str.data = 8;
2037 smp->data.type = SMP_T_STR;
2038 smp->flags = SMP_F_CONST;
2039 return 1;
2040
2041}
2042
2043/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2044 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2045 */
2046static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2047{
2048 struct http_txn *txn = smp->strm->txn;
2049
2050 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2051 return 0;
2052
2053 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2054 smp->data.u.str.area = "HTTP/1.1";
2055 else
2056 smp->data.u.str.area = "HTTP/1.0";
2057
2058 smp->data.u.str.data = 8;
2059 smp->data.type = SMP_T_STR;
2060 smp->flags = SMP_F_CONST;
2061 return 1;
2062
2063}
2064
2065/* Iterate over all cookies present in a message. The context is stored in
2066 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2067 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2068 * the direction, multiple cookies may be parsed on the same line or not.
2069 * The cookie name is in args and the name length in args->data.str.len.
2070 * Accepts exactly 1 argument of type string. If the input options indicate
2071 * that no iterating is desired, then only last value is fetched if any.
2072 * The returned sample is of type CSTR. Can be used to parse cookies in other
2073 * files.
2074 */
2075static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2076{
Willy Tarreau79e57332018-10-02 16:01:16 +02002077 int occ = 0;
2078 int found = 0;
2079
2080 if (!args || args->type != ARGT_STR)
2081 return 0;
2082
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002083 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2084 /* HTX version */
2085 struct htx *htx = smp_prefetch_htx(smp, args);
2086 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2087 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002088
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002089 if (!ctx) {
2090 /* first call */
2091 ctx = &static_http_hdr_ctx;
2092 ctx->blk = NULL;
2093 smp->ctx.a[2] = ctx;
2094 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002095
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002096 if (!htx)
2097 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002098
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002099 hdr = (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2100 ? ist("Cookie")
2101 : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002102
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002103 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2104 /* no explicit occurrence and single fetch => last cookie by default */
2105 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02002106
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002107 /* OK so basically here, either we want only one value and it's the
2108 * last one, or we want to iterate over all of them and we fetch the
2109 * next one.
Willy Tarreau79e57332018-10-02 16:01:16 +02002110 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002111
2112 if (!(smp->flags & SMP_F_NOT_LAST)) {
2113 /* search for the header from the beginning, we must first initialize
2114 * the search parameters.
2115 */
2116 smp->ctx.a[0] = NULL;
2117 ctx->blk = NULL;
2118 }
2119
2120 smp->flags |= SMP_F_VOL_HDR;
2121 while (1) {
2122 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2123 if (!smp->ctx.a[0]) {
2124 if (!http_find_header(htx, hdr, ctx, 0))
2125 goto out;
2126
2127 if (ctx->value.len < args->data.str.data + 1)
2128 continue;
2129
2130 smp->ctx.a[0] = ctx->value.ptr;
2131 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2132 }
2133
2134 smp->data.type = SMP_T_STR;
2135 smp->flags |= SMP_F_CONST;
2136 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2137 args->data.str.area, args->data.str.data,
2138 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2139 &smp->data.u.str.area,
2140 &smp->data.u.str.data);
2141 if (smp->ctx.a[0]) {
2142 found = 1;
2143 if (occ >= 0) {
2144 /* one value was returned into smp->data.u.str.{str,len} */
2145 smp->flags |= SMP_F_NOT_LAST;
2146 return 1;
2147 }
2148 }
2149 /* if we're looking for last occurrence, let's loop */
2150 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002151 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002152 else {
2153 /* LEGACY version */
2154 struct http_txn *txn;
2155 struct hdr_idx *idx;
2156 struct hdr_ctx *ctx = smp->ctx.a[2];
2157 const struct http_msg *msg;
2158 const char *hdr_name;
2159 int hdr_name_len;
2160 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002161
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002162 if (!ctx) {
2163 /* first call */
2164 ctx = &static_hdr_ctx;
2165 ctx->idx = 0;
2166 smp->ctx.a[2] = ctx;
2167 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002168
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002169 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02002170
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002171 txn = smp->strm->txn;
2172 idx = &smp->strm->txn->hdr_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +02002173
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002174 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
2175 msg = &txn->req;
2176 hdr_name = "Cookie";
2177 hdr_name_len = 6;
2178 } else {
2179 msg = &txn->rsp;
2180 hdr_name = "Set-Cookie";
2181 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002182 }
2183
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002184 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2185 /* no explicit occurrence and single fetch => last cookie by default */
2186 occ = -1;
2187
2188 /* OK so basically here, either we want only one value and it's the
2189 * last one, or we want to iterate over all of them and we fetch the
2190 * next one.
2191 */
2192
2193 sol = ci_head(msg->chn);
2194 if (!(smp->flags & SMP_F_NOT_LAST)) {
2195 /* search for the header from the beginning, we must first initialize
2196 * the search parameters.
2197 */
2198 smp->ctx.a[0] = NULL;
2199 ctx->idx = 0;
2200 }
2201
2202 smp->flags |= SMP_F_VOL_HDR;
2203
2204 while (1) {
2205 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2206 if (!smp->ctx.a[0]) {
2207 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2208 goto out;
2209
2210 if (ctx->vlen < args->data.str.data + 1)
2211 continue;
2212
2213 smp->ctx.a[0] = ctx->line + ctx->val;
2214 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2215 }
2216
2217 smp->data.type = SMP_T_STR;
2218 smp->flags |= SMP_F_CONST;
2219 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2220 args->data.str.area, args->data.str.data,
2221 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2222 &smp->data.u.str.area, &smp->data.u.str.data);
2223 if (smp->ctx.a[0]) {
2224 found = 1;
2225 if (occ >= 0) {
2226 /* one value was returned into smp->data.u.str.{str,len} */
2227 smp->flags |= SMP_F_NOT_LAST;
2228 return 1;
2229 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002230 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002231 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002232 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002233 }
2234 /* all cookie headers and values were scanned. If we're looking for the
2235 * last occurrence, we may return it now.
2236 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002237 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002238 smp->flags &= ~SMP_F_NOT_LAST;
2239 return found;
2240}
2241
2242/* Iterate over all cookies present in a request to count how many occurrences
2243 * match the name in args and args->data.str.len. If <multi> is non-null, then
2244 * multiple cookies may be parsed on the same line. The returned sample is of
2245 * type UINT. Accepts exactly 1 argument of type string.
2246 */
2247static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2248{
Willy Tarreau79e57332018-10-02 16:01:16 +02002249 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002250 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002251
2252 if (!args || args->type != ARGT_STR)
2253 return 0;
2254
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002255 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2256 /* HTX version */
2257 struct htx *htx = smp_prefetch_htx(smp, args);
2258 struct http_hdr_ctx ctx;
2259 struct ist hdr;
2260
2261 if (!htx)
2262 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002263
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002264 hdr = (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2265 ? ist("Cookie")
2266 : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002267
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002268 val_end = val_beg = NULL;
2269 ctx.blk = NULL;
2270 cnt = 0;
2271 while (1) {
2272 /* Note: val_beg == NULL every time we need to fetch a new header */
2273 if (!val_beg) {
2274 if (!http_find_header(htx, hdr, &ctx, 0))
2275 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002276
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002277 if (ctx.value.len < args->data.str.data + 1)
2278 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002279
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002280 val_beg = ctx.value.ptr;
2281 val_end = val_beg + ctx.value.len;
2282 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002283
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002284 smp->data.type = SMP_T_STR;
2285 smp->flags |= SMP_F_CONST;
2286 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2287 args->data.str.area, args->data.str.data,
2288 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2289 &smp->data.u.str.area,
2290 &smp->data.u.str.data))) {
2291 cnt++;
2292 }
2293 }
2294 }
2295 else {
2296 /* LEGACY version */
2297 struct http_txn *txn;
2298 struct hdr_idx *idx;
2299 struct hdr_ctx ctx;
2300 const struct http_msg *msg;
2301 const char *hdr_name;
2302 int hdr_name_len;
2303 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002304
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002305 CHECK_HTTP_MESSAGE_FIRST();
2306
2307 txn = smp->strm->txn;
2308 idx = &smp->strm->txn->hdr_idx;
2309
2310 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
2311 msg = &txn->req;
2312 hdr_name = "Cookie";
2313 hdr_name_len = 6;
2314 } else {
2315 msg = &txn->rsp;
2316 hdr_name = "Set-Cookie";
2317 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002318 }
2319
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002320 sol = ci_head(msg->chn);
2321 val_end = val_beg = NULL;
2322 ctx.idx = 0;
2323 cnt = 0;
2324
2325 while (1) {
2326 /* Note: val_beg == NULL every time we need to fetch a new header */
2327 if (!val_beg) {
2328 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2329 break;
2330
2331 if (ctx.vlen < args->data.str.data + 1)
2332 continue;
2333
2334 val_beg = ctx.line + ctx.val;
2335 val_end = val_beg + ctx.vlen;
2336 }
2337
2338 smp->data.type = SMP_T_STR;
2339 smp->flags |= SMP_F_CONST;
2340 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2341 args->data.str.area, args->data.str.data,
2342 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2343 &smp->data.u.str.area, &smp->data.u.str.data))) {
2344 cnt++;
2345 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002346 }
2347 }
2348
2349 smp->data.type = SMP_T_SINT;
2350 smp->data.u.sint = cnt;
2351 smp->flags |= SMP_F_VOL_HDR;
2352 return 1;
2353}
2354
2355/* Fetch an cookie's integer value. The integer value is returned. It
2356 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2357 */
2358static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2359{
2360 int ret = smp_fetch_cookie(args, smp, kw, private);
2361
2362 if (ret > 0) {
2363 smp->data.type = SMP_T_SINT;
2364 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2365 smp->data.u.str.data);
2366 }
2367
2368 return ret;
2369}
2370
2371/************************************************************************/
2372/* The code below is dedicated to sample fetches */
2373/************************************************************************/
2374
2375/* This scans a URL-encoded query string. It takes an optionally wrapping
2376 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2377 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2378 * pointers are updated for next iteration before leaving.
2379 */
2380static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2381{
2382 const char *vstart, *vend;
2383 struct buffer *temp;
2384 const char **chunks = (const char **)smp->ctx.a;
2385
2386 if (!http_find_next_url_param(chunks, name, name_len,
2387 &vstart, &vend, delim))
2388 return 0;
2389
2390 /* Create sample. If the value is contiguous, return the pointer as CONST,
2391 * if the value is wrapped, copy-it in a buffer.
2392 */
2393 smp->data.type = SMP_T_STR;
2394 if (chunks[2] &&
2395 vstart >= chunks[0] && vstart <= chunks[1] &&
2396 vend >= chunks[2] && vend <= chunks[3]) {
2397 /* Wrapped case. */
2398 temp = get_trash_chunk();
2399 memcpy(temp->area, vstart, chunks[1] - vstart);
2400 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2401 vend - chunks[2]);
2402 smp->data.u.str.area = temp->area;
2403 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2404 } else {
2405 /* Contiguous case. */
2406 smp->data.u.str.area = (char *)vstart;
2407 smp->data.u.str.data = vend - vstart;
2408 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2409 }
2410
2411 /* Update context, check wrapping. */
2412 chunks[0] = vend;
2413 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2414 chunks[1] = chunks[3];
2415 chunks[2] = NULL;
2416 }
2417
2418 if (chunks[0] < chunks[1])
2419 smp->flags |= SMP_F_NOT_LAST;
2420
2421 return 1;
2422}
2423
2424/* This function iterates over each parameter of the query string. It uses
2425 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2426 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2427 * An optional parameter name is passed in args[0], otherwise any parameter is
2428 * considered. It supports an optional delimiter argument for the beginning of
2429 * the string in args[1], which defaults to "?".
2430 */
2431static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2432{
Willy Tarreau79e57332018-10-02 16:01:16 +02002433 char delim = '?';
2434 const char *name;
2435 int name_len;
2436
2437 if (!args ||
2438 (args[0].type && args[0].type != ARGT_STR) ||
2439 (args[1].type && args[1].type != ARGT_STR))
2440 return 0;
2441
2442 name = "";
2443 name_len = 0;
2444 if (args->type == ARGT_STR) {
2445 name = args->data.str.area;
2446 name_len = args->data.str.data;
2447 }
2448
2449 if (args[1].type)
2450 delim = *args[1].data.str.area;
2451
2452 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002453 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2454 /* HTX version */
2455 struct htx *htx = smp_prefetch_htx(smp, args);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002456 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002457
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002458 if (!htx)
2459 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002460
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002461 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002462 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 +02002463 if (!smp->ctx.a[0])
2464 return 0;
2465
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002466 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002467 }
2468 else {
2469 /* LEGACY version */
2470 struct http_msg *msg;
2471
2472 CHECK_HTTP_MESSAGE_FIRST();
2473
2474 msg = &smp->strm->txn->req;
2475
2476 smp->ctx.a[0] = http_find_param_list(ci_head(msg->chn) + msg->sl.rq.u,
2477 msg->sl.rq.u_l, delim);
2478 if (!smp->ctx.a[0])
2479 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002480
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002481 smp->ctx.a[1] = ci_head(msg->chn) + msg->sl.rq.u + msg->sl.rq.u_l;
2482 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002483
2484 /* Assume that the context is filled with NULL pointer
2485 * before the first call.
2486 * smp->ctx.a[2] = NULL;
2487 * smp->ctx.a[3] = NULL;
2488 */
2489 }
2490
2491 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2492}
2493
2494/* This function iterates over each parameter of the body. This requires
2495 * that the body has been waited for using http-buffer-request. It uses
2496 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2497 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2498 * optional second part if the body wraps at the end of the buffer. An optional
2499 * parameter name is passed in args[0], otherwise any parameter is considered.
2500 */
2501static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2502{
Willy Tarreau79e57332018-10-02 16:01:16 +02002503 const char *name;
2504 int name_len;
2505
2506 if (!args || (args[0].type && args[0].type != ARGT_STR))
2507 return 0;
2508
2509 name = "";
2510 name_len = 0;
2511 if (args[0].type == ARGT_STR) {
2512 name = args[0].data.str.area;
2513 name_len = args[0].data.str.data;
2514 }
2515
2516 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002517 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2518 /* HTX version */
2519 struct htx *htx = smp_prefetch_htx(smp, args);
2520 struct buffer *temp;
2521 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002522
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002523 if (!htx)
2524 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002525
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002526 temp = get_trash_chunk();
2527 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
2528 struct htx_blk *blk = htx_get_blk(htx, pos);
2529 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002530
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002531 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
2532 break;
2533 if (type == HTX_BLK_DATA) {
2534 if (!htx_data_to_str(htx_get_blk_value(htx, blk), temp, 0))
2535 return 0;
2536 }
2537 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002538
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002539 smp->ctx.a[0] = temp->area;
2540 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002541
2542 /* Assume that the context is filled with NULL pointer
2543 * before the first call.
2544 * smp->ctx.a[2] = NULL;
2545 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002546 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002547 }
2548 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002549 /* LEGACY version */
2550 struct http_msg *msg;
2551 unsigned long len;
2552 unsigned long block1;
2553 char *body;
2554
2555 CHECK_HTTP_MESSAGE_FIRST();
2556
2557 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ)
2558 msg = &smp->strm->txn->req;
2559 else
2560 msg = &smp->strm->txn->rsp;
2561
2562 len = http_body_bytes(msg);
2563 body = c_ptr(msg->chn, -http_data_rewind(msg));
2564
2565 block1 = len;
2566 if (block1 > b_wrap(&msg->chn->buf) - body)
2567 block1 = b_wrap(&msg->chn->buf) - body;
2568
2569 if (block1 == len) {
2570 /* buffer is not wrapped (or empty) */
2571 smp->ctx.a[0] = body;
2572 smp->ctx.a[1] = body + len;
2573
2574 /* Assume that the context is filled with NULL pointer
2575 * before the first call.
2576 * smp->ctx.a[2] = NULL;
2577 * smp->ctx.a[3] = NULL;
2578 */
2579 }
2580 else {
2581 /* buffer is wrapped, we need to defragment it */
2582 smp->ctx.a[0] = body;
2583 smp->ctx.a[1] = body + block1;
2584 smp->ctx.a[2] = b_orig(&msg->chn->buf);
2585 smp->ctx.a[3] = b_orig(&msg->chn->buf) + ( len - block1 );
2586 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002587 }
2588 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002589
Willy Tarreau79e57332018-10-02 16:01:16 +02002590 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2591}
2592
2593/* Return the signed integer value for the specified url parameter (see url_param
2594 * above).
2595 */
2596static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2597{
2598 int ret = smp_fetch_url_param(args, smp, kw, private);
2599
2600 if (ret > 0) {
2601 smp->data.type = SMP_T_SINT;
2602 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2603 smp->data.u.str.data);
2604 }
2605
2606 return ret;
2607}
2608
2609/* This produces a 32-bit hash of the concatenation of the first occurrence of
2610 * the Host header followed by the path component if it begins with a slash ('/').
2611 * This means that '*' will not be added, resulting in exactly the first Host
2612 * entry. If no Host header is found, then the path is used. The resulting value
2613 * is hashed using the url hash followed by a full avalanche hash and provides a
2614 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2615 * high-traffic sites without having to store whole paths.
2616 * this differs from the base32 functions in that it includes the url parameters
2617 * as well as the path
2618 */
2619static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2620{
Willy Tarreau79e57332018-10-02 16:01:16 +02002621 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002622
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002623 if (IS_HTX_SMP(smp) || (smp->px->mode == PR_MODE_TCP)) {
2624 /* HTX version */
2625 struct htx *htx = smp_prefetch_htx(smp, args);
2626 struct http_hdr_ctx ctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002627 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002628 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002629
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002630 if (!htx)
2631 return 0;
2632
2633 ctx.blk = NULL;
2634 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2635 /* OK we have the header value in ctx.value */
2636 while (ctx.value.len--)
2637 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2638 }
2639
2640 /* now retrieve the path */
2641 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002642 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002643 while (path.len > 0 && *(path.ptr) != '?') {
2644 path.ptr++;
2645 path.len--;
2646 }
2647 if (path.len && *(path.ptr) == '/') {
2648 while (path.len--)
2649 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2650 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002651 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002652 else {
2653 /* LEGACY version */
2654 struct http_txn *txn;
2655 struct hdr_ctx ctx;
2656 char *ptr, *beg, *end;
2657 int len;
2658
2659 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau79e57332018-10-02 16:01:16 +02002660
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002661 txn = smp->strm->txn;
2662 ctx.idx = 0;
2663 if (http_find_header2("Host", 4, ci_head(txn->req.chn), &txn->hdr_idx, &ctx)) {
2664 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2665 ptr = ctx.line + ctx.val;
2666 len = ctx.vlen;
2667 while (len--)
2668 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2669 }
2670
2671 /* now retrieve the path */
2672 end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
2673 beg = http_txn_get_path(txn);
2674 if (!beg)
2675 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002676
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002677 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002678
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002679 if (beg < ptr && *beg == '/') {
2680 while (beg < ptr)
2681 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2682 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002683 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002684
Willy Tarreau79e57332018-10-02 16:01:16 +02002685 hash = full_hash(hash);
2686
2687 smp->data.type = SMP_T_SINT;
2688 smp->data.u.sint = hash;
2689 smp->flags = SMP_F_VOL_1ST;
2690 return 1;
2691}
2692
2693/* This concatenates the source address with the 32-bit hash of the Host and
2694 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2695 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2696 * on the source address length. The URL hash is stored before the address so
2697 * that in environments where IPv6 is insignificant, truncating the output to
2698 * 8 bytes would still work.
2699 */
2700static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2701{
2702 struct buffer *temp;
2703 struct connection *cli_conn = objt_conn(smp->sess->origin);
2704
2705 if (!cli_conn)
2706 return 0;
2707
2708 if (!smp_fetch_url32(args, smp, kw, private))
2709 return 0;
2710
2711 temp = get_trash_chunk();
2712 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2713 temp->data += sizeof(unsigned int);
2714
2715 switch (cli_conn->addr.from.ss_family) {
2716 case AF_INET:
2717 memcpy(temp->area + temp->data,
2718 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2719 4);
2720 temp->data += 4;
2721 break;
2722 case AF_INET6:
2723 memcpy(temp->area + temp->data,
2724 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2725 16);
2726 temp->data += 16;
2727 break;
2728 default:
2729 return 0;
2730 }
2731
2732 smp->data.u.str = *temp;
2733 smp->data.type = SMP_T_BIN;
2734 return 1;
2735}
2736
2737/************************************************************************/
2738/* Other utility functions */
2739/************************************************************************/
2740
2741/* This function is used to validate the arguments passed to any "hdr" fetch
2742 * keyword. These keywords support an optional positive or negative occurrence
2743 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2744 * is assumed that the types are already the correct ones. Returns 0 on error,
2745 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2746 * error message in case of error, that the caller is responsible for freeing.
2747 * The initial location must either be freeable or NULL.
2748 * Note: this function's pointer is checked from Lua.
2749 */
2750int val_hdr(struct arg *arg, char **err_msg)
2751{
2752 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2753 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2754 return 0;
2755 }
2756 return 1;
2757}
2758
2759/************************************************************************/
2760/* All supported sample fetch keywords must be declared here. */
2761/************************************************************************/
2762
2763/* Note: must not be declared <const> as its list will be overwritten */
2764static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2765 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2766 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2767 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2768
2769 /* capture are allocated and are permanent in the stream */
2770 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2771
2772 /* retrieve these captures from the HTTP logs */
2773 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2774 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2775 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2776
2777 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2778 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2779
2780 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2781 * are only here to match the ACL's name, are request-only and are used
2782 * for ACL compatibility only.
2783 */
2784 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2785 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
2786 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2787 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2788
2789 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2790 * only here to match the ACL's name, are request-only and are used for
2791 * ACL compatibility only.
2792 */
2793 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
2794 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2795 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2796 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2797
2798 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2799 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2800 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2801 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2802 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2803 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2804
2805 /* HTTP protocol on the request path */
2806 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2807 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2808
2809 /* HTTP version on the request path */
2810 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2811 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2812
2813 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2814 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2815 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2816 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2817
2818 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2819 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2820
2821 /* HTTP version on the response path */
2822 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2823 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2824
2825 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2826 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2827 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2828 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2829
2830 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2831 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2832 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2833 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2834 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2835 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2836 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2837
2838 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2839 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2840 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2841 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2842
2843 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2844 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2845 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2846 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2847 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2848 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2849 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2850
2851 /* scook is valid only on the response and is used for ACL compatibility */
2852 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2853 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2854 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2855 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2856
2857 /* shdr is valid only on the response and is used for ACL compatibility */
2858 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2859 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2860 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2861 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2862
2863 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2864 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2865 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2866 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2867 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2868 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2869 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2870 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2871 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2872 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2873 { /* END */ },
2874}};
2875
Willy Tarreau0108d902018-11-25 19:14:37 +01002876INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002877
2878/*
2879 * Local variables:
2880 * c-indent-level: 8
2881 * c-basic-offset: 8
2882 * End:
2883 */