blob: 3e6f4d1bc03cbf66b25aee2aefd8d270e7b2b3f9 [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
2 * HTTP samples fetching
3 *
4 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/types.h>
14
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
18
19#include <common/base64.h>
20#include <common/chunk.h>
21#include <common/compat.h>
22#include <common/config.h>
23#include <common/debug.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010024#include <common/h1.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020025#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010026#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010027#include <common/initcall.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020028#include <common/memory.h>
29#include <common/standard.h>
30#include <common/version.h>
31
32#include <types/global.h>
33
34#include <proto/arg.h>
35#include <proto/auth.h>
Willy Tarreau538746a2018-12-11 10:59:20 +010036#include <proto/hdr_idx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020037#include <proto/http_fetch.h>
Christopher Fauletef453ed2018-10-24 21:39:27 +020038#include <proto/http_htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020039#include <proto/log.h>
40#include <proto/obj_type.h>
41#include <proto/proto_http.h>
42#include <proto/sample.h>
43#include <proto/stream.h>
44
45
46/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
47static THREAD_LOCAL struct hdr_ctx static_hdr_ctx;
Christopher Fauletef453ed2018-10-24 21:39:27 +020048static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
49
Christopher Faulet89dc4992019-04-17 12:02:59 +020050#define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL)
51#define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL)
Willy Tarreau79e57332018-10-02 16:01:16 +020052
53/*
54 * Returns the data from Authorization header. Function may be called more
55 * than once so data is stored in txn->auth_data. When no header is found
56 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
57 * searching again for something we are unable to find anyway. However, if
58 * the result if valid, the cache is not reused because we would risk to
59 * have the credentials overwritten by another stream in parallel.
60 */
61
Christopher Faulet311c7ea2018-10-24 21:41:55 +020062static int get_http_auth(struct sample *smp)
Willy Tarreau79e57332018-10-02 16:01:16 +020063{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020064 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020065 struct http_txn *txn = s->txn;
66 struct buffer auth_method;
Willy Tarreau79e57332018-10-02 16:01:16 +020067 char *h, *p;
68 int len;
69
70#ifdef DEBUG_AUTH
71 printf("Auth for stream %p: %d\n", s, txn->auth.method);
72#endif
Willy Tarreau79e57332018-10-02 16:01:16 +020073 if (txn->auth.method == HTTP_AUTH_WRONG)
74 return 0;
75
76 txn->auth.method = HTTP_AUTH_WRONG;
77
Christopher Faulet311c7ea2018-10-24 21:41:55 +020078 if (IS_HTX_STRM(s) || (smp->px->mode == PR_MODE_TCP)) {
79 /* HTX version */
Christopher Faulet27ba2dc2018-12-05 11:53:24 +010080 struct htx *htx = htxbuf(&s->req.buf);
Christopher Faulet311c7ea2018-10-24 21:41:55 +020081 struct http_hdr_ctx ctx = { .blk = NULL };
82 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020083
Christopher Faulet311c7ea2018-10-24 21:41:55 +020084 if (txn->flags & TX_USE_PX_CONN)
85 hdr = ist("Proxy-Authorization");
86 else
87 hdr = ist("Authorization");
88
Christopher Faulet311c7ea2018-10-24 21:41:55 +020089 ctx.blk = NULL;
90 if (!http_find_header(htx, hdr, &ctx, 0))
91 return 0;
92
93 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
94 len = p - ctx.value.ptr;
95 if (!p || len <= 0)
96 return 0;
97
98 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
99 return 0;
100
101 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200102 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200103 else {
104 /* LEGACY version */
105 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau79e57332018-10-02 16:01:16 +0200106
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200107 if (txn->flags & TX_USE_PX_CONN) {
108 h = "Proxy-Authorization";
109 len = strlen(h);
110 } else {
111 h = "Authorization";
112 len = strlen(h);
113 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200114
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200115 if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx))
116 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200117
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200118 h = ctx.line + ctx.val;
Willy Tarreau79e57332018-10-02 16:01:16 +0200119
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200120 p = memchr(h, ' ', ctx.vlen);
121 len = p - h;
122 if (!p || len <= 0)
123 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200124
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200125 if (chunk_initlen(&auth_method, h, 0, len) != 1)
126 return 0;
127
128 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
129 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200130
131 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
132 struct buffer *http_auth = get_trash_chunk();
133
134 len = base64dec(txn->auth.method_data.area,
135 txn->auth.method_data.data,
136 http_auth->area, global.tune.bufsize - 1);
137
138 if (len < 0)
139 return 0;
140
141
142 http_auth->area[len] = '\0';
143
144 p = strchr(http_auth->area, ':');
145
146 if (!p)
147 return 0;
148
149 txn->auth.user = http_auth->area;
150 *p = '\0';
151 txn->auth.pass = p+1;
152
153 txn->auth.method = HTTP_AUTH_BASIC;
154 return 1;
155 }
156
157 return 0;
158}
159
160/* This function ensures that the prerequisites for an L7 fetch are ready,
161 * which means that a request or response is ready. If some data is missing,
162 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200163 * to extract data from L7. If <vol> is non-null during a prefetch, another
164 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200165 *
166 * The function returns :
167 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
168 * decide whether or not an HTTP message is present ;
169 * NULL if the requested data cannot be fetched or if it is certain that
170 * we'll never have any HTTP message there ;
171 * The HTX message if ready
172 */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200173struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200174{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200175 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200176 struct http_txn *txn = NULL;
177 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200178 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100179 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200180
181 /* Note: it is possible that <s> is NULL when called before stream
182 * initialization (eg: tcp-request connection), so this function is the
183 * one responsible for guarding against this case for all HTTP users.
184 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200185 if (!s || !chn)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200186 return NULL;
187
188 if (!s->txn) {
189 if (unlikely(!http_alloc_txn(s)))
190 return NULL; /* not enough memory */
191 http_init_txn(s);
192 txn = s->txn;
193 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200194 txn = s->txn;
195 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
196 smp->data.type = SMP_T_BOOL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200197
Christopher Fauleteca88542019-04-03 10:12:42 +0200198 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200199 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200200
Christopher Faulet89dc4992019-04-17 12:02:59 +0200201 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
202 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200203
Christopher Faulet89dc4992019-04-17 12:02:59 +0200204 if (msg->msg_state < HTTP_MSG_BODY) {
205 /* Analyse not yet started */
Christopher Fauletef453ed2018-10-24 21:39:27 +0200206 if (htx_is_empty(htx) || htx_get_tail_type(htx) < HTX_BLK_EOH) {
207 /* Parsing is done by the mux, just wait */
208 smp->flags |= SMP_F_MAY_CHANGE;
209 return NULL;
210 }
211 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200212 sl = http_find_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200213 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200214 /* The start-line was already forwarded, it is too late to fetch anything */
215 return NULL;
216 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200217 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200218 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200219 struct buffer *buf;
220 struct h1m h1m;
221 struct http_hdr hdrs[MAX_HTTP_HDR];
222 union h1_sl h1sl;
223 unsigned int flags = HTX_FL_NONE;
224 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200225
Christopher Faulet89dc4992019-04-17 12:02:59 +0200226 /* no HTTP fetch on the response in TCP mode */
227 if (chn->flags & CF_ISRESP)
228 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200229
Christopher Faulet89dc4992019-04-17 12:02:59 +0200230 /* Now we are working on the request only */
231 buf = &chn->buf;
232 if (b_head(buf) + b_data(buf) > b_wrap(buf))
233 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200234
Christopher Faulet89dc4992019-04-17 12:02:59 +0200235 h1m_init_req(&h1m);
236 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
237 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
238 if (ret <= 0) {
239 /* Invalid or too big*/
240 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200241 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100242
Christopher Faulet89dc4992019-04-17 12:02:59 +0200243 /* wait for a full request */
244 smp->flags |= SMP_F_MAY_CHANGE;
245 return NULL;
246 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100247
Christopher Faulet89dc4992019-04-17 12:02:59 +0200248 /* OK we just got a valid HTTP mesage. We have to convert it
249 * into an HTX message.
250 */
251 if (unlikely(h1sl.rq.v.len == 0)) {
252 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
253 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200254 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200255 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200256 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200257
258 /* Set HTX start-line flags */
259 if (h1m.flags & H1_MF_VER_11)
260 flags |= HTX_SL_F_VER_11;
261 if (h1m.flags & H1_MF_XFER_ENC)
262 flags |= HTX_SL_F_XFER_ENC;
263 flags |= HTX_SL_F_XFER_LEN;
264 if (h1m.flags & H1_MF_CHNK)
265 flags |= HTX_SL_F_CHNK;
266 else if (h1m.flags & H1_MF_CLEN)
267 flags |= HTX_SL_F_CLEN;
268
269 htx = htx_from_buf(get_trash_chunk());
270 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
271 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200272 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200273 }
274
275 /* OK we just got a valid HTTP message. If not already done by
276 * HTTP analyzers, we have some minor preparation to perform so
277 * that further checks can rely on HTTP tests.
278 */
279 if (sl && msg->msg_state < HTTP_MSG_BODY) {
280 if (!(chn->flags & CF_ISRESP)) {
281 txn->meth = sl->info.req.meth;
282 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
283 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200284 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200285 else
286 txn->status = sl->info.res.status;
287 if (sl->flags & HTX_SL_F_VER_11)
288 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200289 }
290
291 /* everything's OK */
292 smp->data.u.sint = 1;
293 return htx;
294}
295
296/* This function ensures that the prerequisites for an L7 fetch are ready,
297 * which means that a request or response is ready. If some data is missing,
298 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau79e57332018-10-02 16:01:16 +0200299 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
300 * another test is made to ensure the required information is not gone.
301 *
302 * The function returns :
303 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
304 * decide whether or not an HTTP message is present ;
305 * 0 if the requested data cannot be fetched or if it is certain that
306 * we'll never have any HTTP message there ;
307 * 1 if an HTTP message is ready
308 */
309int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Christopher Faulet89dc4992019-04-17 12:02:59 +0200310 struct channel *chn, struct sample *smp, int req_vol)
Willy Tarreau79e57332018-10-02 16:01:16 +0200311{
312 struct http_txn *txn;
313 struct http_msg *msg;
314
315 /* Note: it is possible that <s> is NULL when called before stream
316 * initialization (eg: tcp-request connection), so this function is the
317 * one responsible for guarding against this case for all HTTP users.
318 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200319 if (!s || !chn)
Willy Tarreau79e57332018-10-02 16:01:16 +0200320 return 0;
321
322 if (!s->txn) {
323 if (unlikely(!http_alloc_txn(s)))
324 return 0; /* not enough memory */
325 http_init_txn(s);
326 }
327 txn = s->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200328 smp->data.type = SMP_T_BOOL;
329
Christopher Faulet89dc4992019-04-17 12:02:59 +0200330 if (chn->flags & CF_ISRESP) {
331 /* Check for a dependency on a response */
332 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
333 smp->flags |= SMP_F_MAY_CHANGE;
334 return 0;
335 }
336 goto end;
337 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200338
Christopher Faulet89dc4992019-04-17 12:02:59 +0200339 /* Check for a dependency on a request */
340 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200341
Christopher Faulet89dc4992019-04-17 12:02:59 +0200342 if (req_vol && (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
343 return 0; /* data might have moved and indexes changed */
344 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200345
Christopher Faulet89dc4992019-04-17 12:02:59 +0200346 /* If the buffer does not leave enough free space at the end, we must
347 * first realign it.
348 */
349 if (ci_head(chn) > b_orig(&chn->buf) &&
350 ci_head(chn) + ci_data(chn) > b_wrap(&chn->buf) - global.tune.maxrewrite)
351 channel_slow_realign(chn, trash.area);
Willy Tarreau79e57332018-10-02 16:01:16 +0200352
Christopher Faulet89dc4992019-04-17 12:02:59 +0200353 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
354 if (msg->msg_state == HTTP_MSG_ERROR)
355 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200356
Christopher Faulet89dc4992019-04-17 12:02:59 +0200357 /* Try to decode HTTP request */
358 if (likely(msg->next < ci_data(chn)))
359 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau79e57332018-10-02 16:01:16 +0200360
Christopher Faulet89dc4992019-04-17 12:02:59 +0200361 /* Still no valid request ? */
362 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
363 if ((msg->msg_state == HTTP_MSG_ERROR) ||
364 channel_full(chn, global.tune.maxrewrite)) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200365 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200366 }
367 /* wait for final state */
368 smp->flags |= SMP_F_MAY_CHANGE;
369 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200370 }
371
Christopher Faulet89dc4992019-04-17 12:02:59 +0200372 /* OK we just got a valid HTTP message. We have some minor
373 * preparation to perform so that further checks can rely
374 * on HTTP tests.
375 */
376
377 /* If the message was parsed but was too large, we must absolutely
378 * return an error so that it is not processed. At the moment this
379 * cannot happen, but if the parsers are to change in the future,
380 * we want this check to be maintained.
381 */
382 if (unlikely(ci_head(chn) + ci_data(chn) >
383 b_wrap(&chn->buf) - global.tune.maxrewrite)) {
384 msg->err_state = msg->msg_state;
385 msg->msg_state = HTTP_MSG_ERROR;
386 smp->data.u.sint = 1;
387 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200388 }
389
Christopher Faulet89dc4992019-04-17 12:02:59 +0200390 txn->meth = find_http_meth(ci_head(chn), msg->sl.rq.m_l);
391 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
392 s->flags |= SF_REDIRECTABLE;
393
394 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau79e57332018-10-02 16:01:16 +0200395 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200396 }
397
Christopher Faulet89dc4992019-04-17 12:02:59 +0200398 end:
Willy Tarreau79e57332018-10-02 16:01:16 +0200399 /* everything's OK */
400 smp->data.u.sint = 1;
401 return 1;
402}
403
404/* This function fetches the method of current HTTP request and stores
405 * it in the global pattern struct as a chunk. There are two possibilities :
406 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
407 * in <len> and <ptr> is NULL ;
408 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
409 * <len> to its length.
410 * This is intended to be used with pat_match_meth() only.
411 */
412static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
413{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200414 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200415 int meth;
416 struct http_txn *txn;
417
Christopher Faulet46575cd2019-04-17 11:40:30 +0200418 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200419 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200420 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +0200421
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200422 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +0200423 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200424
425 txn = smp->strm->txn;
426 meth = txn->meth;
427 smp->data.type = SMP_T_METH;
428 smp->data.u.meth.meth = meth;
429 if (meth == HTTP_METH_OTHER) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100430 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200431
Christopher Faulet89dc4992019-04-17 12:02:59 +0200432 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200433 /* ensure the indexes are not affected */
434 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200435 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200436 sl = http_find_stline(htx);
437 smp->flags |= SMP_F_CONST;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100438 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
439 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200440 }
441 smp->flags |= SMP_F_VOL_1ST;
442 }
443 else {
444 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200445 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200446
447 txn = smp->strm->txn;
448 meth = txn->meth;
449 smp->data.type = SMP_T_METH;
450 smp->data.u.meth.meth = meth;
451 if (meth == HTTP_METH_OTHER) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200452 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200453 /* ensure the indexes are not affected */
454 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200455 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200456 smp->flags |= SMP_F_CONST;
457 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
458 smp->data.u.meth.str.area = ci_head(txn->req.chn);
459 }
460 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200461 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200462 return 1;
463}
464
465static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
466{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200467 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200468 struct http_txn *txn;
469 char *ptr;
470 int len;
471
Christopher Faulet46575cd2019-04-17 11:40:30 +0200472 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200473 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200474 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100475 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200476
477 if (!htx)
478 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200479
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200480 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100481 len = HTX_SL_REQ_VLEN(sl);
482 ptr = HTX_SL_REQ_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200483 }
484 else {
485 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200486 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200487
488 txn = smp->strm->txn;
489 len = txn->req.sl.rq.v_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200490 ptr = ci_head(chn) + txn->req.sl.rq.v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200491 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200492
493 while ((len-- > 0) && (*ptr++ != '/'));
494 if (len <= 0)
495 return 0;
496
497 smp->data.type = SMP_T_STR;
498 smp->data.u.str.area = ptr;
499 smp->data.u.str.data = len;
500
501 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
502 return 1;
503}
504
505static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
506{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200507 struct channel *chn = SMP_RES_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200508 struct http_txn *txn;
509 char *ptr;
510 int len;
511
Christopher Faulet46575cd2019-04-17 11:40:30 +0200512 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200513 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200514 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100515 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200516
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200517 if (!htx)
518 return 0;
519
520 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100521 len = HTX_SL_RES_VLEN(sl);
522 ptr = HTX_SL_RES_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200523 }
524 else {
525 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200526 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200527
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200528 txn = smp->strm->txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200529 len = txn->rsp.sl.st.v_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200530 ptr = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200531 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200532
533 while ((len-- > 0) && (*ptr++ != '/'));
534 if (len <= 0)
535 return 0;
536
537 smp->data.type = SMP_T_STR;
538 smp->data.u.str.area = ptr;
539 smp->data.u.str.data = len;
540
541 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
542 return 1;
543}
544
545/* 3. Check on Status Code. We manipulate integers here. */
546static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
547{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200548 struct channel *chn = SMP_RES_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200549 struct http_txn *txn;
550 char *ptr;
551 int len;
552
Christopher Faulet46575cd2019-04-17 11:40:30 +0200553 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200554 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200555 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100556 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200557
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200558 if (!htx)
559 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200560
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200561 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100562 len = HTX_SL_RES_CLEN(sl);
563 ptr = HTX_SL_RES_CPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200564 }
565 else {
566 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200567 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200568
569 txn = smp->strm->txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200570 len = txn->rsp.sl.st.c_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200571 ptr = ci_head(chn) + txn->rsp.sl.st.c;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200572 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200573
574 smp->data.type = SMP_T_SINT;
575 smp->data.u.sint = __strl2ui(ptr, len);
576 smp->flags = SMP_F_VOL_1ST;
577 return 1;
578}
579
580static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
581{
582 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
583 return 0;
584
585 if (!smp->strm->unique_id) {
586 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
587 return 0;
588 smp->strm->unique_id[0] = '\0';
589 }
590 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
591 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
592
593 smp->data.type = SMP_T_STR;
594 smp->data.u.str.area = smp->strm->unique_id;
595 smp->flags = SMP_F_CONST;
596 return 1;
597}
598
599/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800600 * empty line which separes headers from the body. This is useful
601 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200602 */
603static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
604{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200605 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200606 struct http_txn *txn;
607
Christopher Faulet46575cd2019-04-17 11:40:30 +0200608 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200609 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200610 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200611 struct buffer *temp;
612 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200613
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200614 if (!htx)
615 return 0;
616 temp = get_trash_chunk();
617 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
618 struct htx_blk *blk = htx_get_blk(htx, pos);
619 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200620
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200621 if (type == HTX_BLK_HDR) {
622 struct ist n = htx_get_blk_name(htx, blk);
623 struct ist v = htx_get_blk_value(htx, blk);
624
Christopher Fauletc59ff232018-12-03 13:58:44 +0100625 if (!htx_hdr_to_h1(n, v, temp))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200626 return 0;
627 }
628 else if (type == HTX_BLK_EOH) {
629 if (!chunk_memcat(temp, "\r\n", 2))
630 return 0;
631 break;
632 }
633 }
634 smp->data.type = SMP_T_STR;
635 smp->data.u.str = *temp;
636
637 }
638 else {
639 /* LEGACY version */
640 struct http_msg *msg;
641 struct hdr_idx *idx;
642
Christopher Faulet89dc4992019-04-17 12:02:59 +0200643 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200644
645 txn = smp->strm->txn;
646 idx = &txn->hdr_idx;
647 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200648
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200649 smp->data.type = SMP_T_STR;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200650 smp->data.u.str.area = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200651 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
Christopher Faulet89dc4992019-04-17 12:02:59 +0200652 (ci_head(chn)[msg->eoh] == '\r');
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200653 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200654 return 1;
655}
656
657/* Returns the header request in a length/value encoded format.
658 * This is useful for exchanges with the SPOE.
659 *
660 * A "length value" is a multibyte code encoding numbers. It uses the
661 * SPOE format. The encoding is the following:
662 *
663 * Each couple "header name" / "header value" is composed
664 * like this:
665 * "length value" "header name bytes"
666 * "length value" "header value bytes"
667 * When the last header is reached, the header name and the header
668 * value are empty. Their length are 0
669 */
670static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
671{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200672 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200673 struct http_txn *txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200674 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200675
Christopher Faulet46575cd2019-04-17 11:40:30 +0200676 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200677 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200678 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200679 struct buffer *temp;
680 char *p, *end;
681 int32_t pos;
682 int ret;
Willy Tarreau79e57332018-10-02 16:01:16 +0200683
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200684 if (!htx)
685 return 0;
686 temp = get_trash_chunk();
687 p = temp->area;
688 end = temp->area + temp->size;
689 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
690 struct htx_blk *blk = htx_get_blk(htx, pos);
691 enum htx_blk_type type = htx_get_blk_type(blk);
692 struct ist n, v;
Willy Tarreau79e57332018-10-02 16:01:16 +0200693
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200694 if (type == HTX_BLK_HDR) {
695 n = htx_get_blk_name(htx,blk);
696 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200697
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200698 /* encode the header name. */
699 ret = encode_varint(n.len, &p, end);
700 if (ret == -1)
701 return 0;
702 if (p + n.len > end)
703 return 0;
704 memcpy(p, n.ptr, n.len);
705 p += n.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200706
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200707 /* encode the header value. */
708 ret = encode_varint(v.len, &p, end);
709 if (ret == -1)
710 return 0;
711 if (p + v.len > end)
712 return 0;
713 memcpy(p, v.ptr, v.len);
714 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200715
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200716 }
717 else if (type == HTX_BLK_EOH) {
718 /* encode the end of the header list with empty
719 * header name and header value.
720 */
721 ret = encode_varint(0, &p, end);
722 if (ret == -1)
723 return 0;
724 ret = encode_varint(0, &p, end);
725 if (ret == -1)
726 return 0;
727 break;
728 }
729 }
730
731 /* Initialise sample data which will be filled. */
732 smp->data.type = SMP_T_BIN;
733 smp->data.u.str.area = temp->area;
734 smp->data.u.str.data = p - temp->area;
735 smp->data.u.str.size = temp->size;
736 }
737 else {
738 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200739 struct hdr_idx *idx;
740 const char *cur_ptr, *cur_next, *p;
741 int old_idx, cur_idx;
742 struct hdr_idx_elem *cur_hdr;
743 const char *hn, *hv;
744 int hnl, hvl;
745 int ret;
746 char *buf;
747 char *end;
748
Christopher Faulet89dc4992019-04-17 12:02:59 +0200749 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200750
751 temp = get_trash_chunk();
752 buf = temp->area;
753 end = temp->area + temp->size;
754
755 txn = smp->strm->txn;
756 idx = &txn->hdr_idx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200757
758 /* Build array of headers. */
759 old_idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200760 cur_next = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200761 while (1) {
762 cur_idx = idx->v[old_idx].next;
763 if (!cur_idx)
764 break;
765 old_idx = cur_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +0200766
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200767 cur_hdr = &idx->v[cur_idx];
768 cur_ptr = cur_next;
769 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
770
771 /* Now we have one full header at cur_ptr of len cur_hdr->len,
772 * and the next header starts at cur_next. We'll check
773 * this header in the list as well as against the default
774 * rule.
775 */
776
777 /* look for ': *'. */
778 hn = cur_ptr;
779 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
780 if (p >= cur_ptr+cur_hdr->len)
781 continue;
782 hnl = p - hn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200783 p++;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200784 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
785 p++;
786 if (p >= cur_ptr + cur_hdr->len)
787 continue;
788 hv = p;
789 hvl = cur_ptr + cur_hdr->len-p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200790
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200791 /* encode the header name. */
792 ret = encode_varint(hnl, &buf, end);
793 if (ret == -1)
794 return 0;
795 if (buf + hnl > end)
796 return 0;
797 memcpy(buf, hn, hnl);
798 buf += hnl;
799
800 /* encode and copy the value. */
801 ret = encode_varint(hvl, &buf, end);
802 if (ret == -1)
803 return 0;
804 if (buf + hvl > end)
805 return 0;
806 memcpy(buf, hv, hvl);
807 buf += hvl;
808 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200809
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200810 /* encode the end of the header list with empty
811 * header name and header value.
812 */
813 ret = encode_varint(0, &buf, end);
Willy Tarreau79e57332018-10-02 16:01:16 +0200814 if (ret == -1)
815 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200816 ret = encode_varint(0, &buf, end);
817 if (ret == -1)
Willy Tarreau79e57332018-10-02 16:01:16 +0200818 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200819
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200820 /* Initialise sample data which will be filled. */
821 smp->data.type = SMP_T_BIN;
822 smp->data.u.str.area = temp->area;
823 smp->data.u.str.data = buf - temp->area;
824 smp->data.u.str.size = temp->size;
825 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200826 return 1;
827}
828
829/* returns the longest available part of the body. This requires that the body
830 * has been waited for using http-buffer-request.
831 */
832static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
833{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200834 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200835 struct buffer *temp;
836
Christopher Faulet46575cd2019-04-17 11:40:30 +0200837 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200838 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200839 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200840 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200841
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200842 if (!htx)
843 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200844
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200845 temp = get_trash_chunk();
846 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
847 struct htx_blk *blk = htx_get_blk(htx, pos);
848 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200849
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200850 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
851 break;
852 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +0100853 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200854 return 0;
855 }
856 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200857
Willy Tarreau79e57332018-10-02 16:01:16 +0200858 smp->data.type = SMP_T_BIN;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200859 smp->data.u.str = *temp;
860 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200861 }
862 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200863 /* LEGACY version */
864 struct http_msg *msg;
865 unsigned long len;
866 unsigned long block1;
867 char *body;
868
Christopher Faulet89dc4992019-04-17 12:02:59 +0200869 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200870
Christopher Faulet89dc4992019-04-17 12:02:59 +0200871 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200872 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200873 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200874
875 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200876 if (block1 > b_wrap(&chn->buf) - body)
877 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200878
879 if (block1 == len) {
880 /* buffer is not wrapped (or empty) */
881 smp->data.type = SMP_T_BIN;
882 smp->data.u.str.area = body;
883 smp->data.u.str.data = len;
884 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
885 }
886 else {
887 /* buffer is wrapped, we need to defragment it */
888 temp = get_trash_chunk();
889 memcpy(temp->area, body, block1);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200890 memcpy(temp->area + block1, b_orig(&chn->buf), len - block1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200891 smp->data.type = SMP_T_BIN;
892 smp->data.u.str.area = temp->area;
893 smp->data.u.str.data = len;
894 smp->flags = SMP_F_VOL_TEST;
895 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200896 }
897 return 1;
898}
899
900
901/* returns the available length of the body. This requires that the body
902 * has been waited for using http-buffer-request.
903 */
904static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
905{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200906 struct channel *chn = SMP_REQ_CHN(smp);
907
Christopher Faulet46575cd2019-04-17 11:40:30 +0200908 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200909 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200910 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +0100911 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100912 unsigned long long len = 0;
913
914 if (!htx)
915 return 0;
916
Dragan Dosen5a606682019-02-14 12:30:53 +0100917 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
918 struct htx_blk *blk = htx_get_blk(htx, pos);
919 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100920
Dragan Dosen5a606682019-02-14 12:30:53 +0100921 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
Christopher Fauletc16317d2018-12-12 14:11:22 +0100922 break;
Dragan Dosen5a606682019-02-14 12:30:53 +0100923 if (type == HTX_BLK_DATA)
924 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100925 }
926
927 smp->data.type = SMP_T_SINT;
928 smp->data.u.sint = len;
929
930 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200931 }
932 else {
933 /* LEGACY version */
934 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200935
Christopher Faulet89dc4992019-04-17 12:02:59 +0200936 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200937
Christopher Faulet89dc4992019-04-17 12:02:59 +0200938 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200939 smp->data.type = SMP_T_SINT;
940 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreau79e57332018-10-02 16:01:16 +0200941
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200942 smp->flags = SMP_F_VOL_TEST;
943 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200944 return 1;
945}
946
947
948/* returns the advertised length of the body, or the advertised size of the
949 * chunks available in the buffer. This requires that the body has been waited
950 * for using http-buffer-request.
951 */
952static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
953{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200954 struct channel *chn = SMP_REQ_CHN(smp);
955
Christopher Faulet46575cd2019-04-17 11:40:30 +0200956 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200957 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200958 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +0100959 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100960 unsigned long long len = 0;
961
962 if (!htx)
963 return 0;
964
Dragan Dosen5a606682019-02-14 12:30:53 +0100965 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
966 struct htx_blk *blk = htx_get_blk(htx, pos);
967 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100968
Dragan Dosen5a606682019-02-14 12:30:53 +0100969 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
Christopher Fauletc16317d2018-12-12 14:11:22 +0100970 break;
Dragan Dosen5a606682019-02-14 12:30:53 +0100971 if (type == HTX_BLK_DATA)
972 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100973 }
974 if (htx->extra != ULLONG_MAX)
975 len += htx->extra;
976
977 smp->data.type = SMP_T_SINT;
978 smp->data.u.sint = len;
979
980 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200981 }
982 else {
983 /* LEGACY version */
984 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200985
Christopher Faulet89dc4992019-04-17 12:02:59 +0200986 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200987
Christopher Faulet89dc4992019-04-17 12:02:59 +0200988 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200989 smp->data.type = SMP_T_SINT;
990 smp->data.u.sint = msg->body_len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200991
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200992 smp->flags = SMP_F_VOL_TEST;
993 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200994 return 1;
995}
996
997
998/* 4. Check on URL/URI. A pointer to the URI is stored. */
999static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
1000{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001001 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001002 struct http_txn *txn;
1003
Christopher Faulet46575cd2019-04-17 11:40:30 +02001004 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001005 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001006 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001007 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001008
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001009 if (!htx)
1010 return 0;
1011 sl = http_find_stline(htx);
1012 smp->data.type = SMP_T_STR;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001013 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
1014 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001015 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1016 }
1017 else {
1018 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001019 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001020
1021 txn = smp->strm->txn;
1022 smp->data.type = SMP_T_STR;
1023 smp->data.u.str.data = txn->req.sl.rq.u_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001024 smp->data.u.str.area = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001025 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1026 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001027 return 1;
1028}
1029
1030static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1031{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001032 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001033 struct http_txn *txn;
1034 struct sockaddr_storage addr;
1035
Christopher Faulet46575cd2019-04-17 11:40:30 +02001036 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001037 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001038 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001039 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001040
1041 if (!htx)
1042 return 0;
1043 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001044 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001045 }
1046 else {
1047 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001048 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001049
1050 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001051 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001052 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001053
Willy Tarreau79e57332018-10-02 16:01:16 +02001054 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1055 return 0;
1056
1057 smp->data.type = SMP_T_IPV4;
1058 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
1059 smp->flags = 0;
1060 return 1;
1061}
1062
1063static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
1064{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001065 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001066 struct http_txn *txn;
1067 struct sockaddr_storage addr;
1068
Christopher Faulet46575cd2019-04-17 11:40:30 +02001069 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001070 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001071 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001072 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001073
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001074 if (!htx)
1075 return 0;
1076 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001077 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001078 }
1079 else {
1080 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001081 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001082
1083 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001084 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001085 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001086 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1087 return 0;
1088
1089 smp->data.type = SMP_T_SINT;
1090 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
1091 smp->flags = 0;
1092 return 1;
1093}
1094
1095/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1096 * Accepts an optional argument of type string containing the header field name,
1097 * and an optional argument of type signed or unsigned integer to request an
1098 * explicit occurrence of the header. Note that in the event of a missing name,
1099 * headers are considered from the first one. It does not stop on commas and
1100 * returns full lines instead (useful for User-Agent or Date for example).
1101 */
1102static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1103{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001104 /* possible keywords: req.fhdr, res.fhdr */
1105 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001106 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001107
Christopher Faulet46575cd2019-04-17 11:40:30 +02001108 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001109 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001110 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001111 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1112 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +02001113
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001114 if (!ctx) {
1115 /* first call */
1116 ctx = &static_http_hdr_ctx;
1117 ctx->blk = NULL;
1118 smp->ctx.a[0] = ctx;
1119 }
1120
1121 if (args) {
1122 if (args[0].type != ARGT_STR)
1123 return 0;
1124 name.ptr = args[0].data.str.area;
1125 name.len = args[0].data.str.data;
1126
1127 if (args[1].type == ARGT_SINT)
1128 occ = args[1].data.sint;
1129 }
1130
1131 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001132 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001133
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001134 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1135 /* search for header from the beginning */
1136 ctx->blk = NULL;
1137
1138 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1139 /* no explicit occurrence and single fetch => last header by default */
1140 occ = -1;
1141
1142 if (!occ)
1143 /* prepare to report multiple occurrences for ACL fetches */
1144 smp->flags |= SMP_F_NOT_LAST;
1145
1146 smp->data.type = SMP_T_STR;
1147 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1148 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1149 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001150 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001151 else {
1152 /* LEGACY version */
1153 struct hdr_idx *idx;
1154 struct hdr_ctx *ctx = smp->ctx.a[0];
1155 const struct http_msg *msg;
1156 const char *name_str = NULL;
1157 int name_len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001158
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001159 if (!ctx) {
1160 /* first call */
1161 ctx = &static_hdr_ctx;
1162 ctx->idx = 0;
1163 smp->ctx.a[0] = ctx;
1164 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001165
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001166 if (args) {
1167 if (args[0].type != ARGT_STR)
1168 return 0;
1169 name_str = args[0].data.str.area;
1170 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001171
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001172 if (args[1].type == ARGT_SINT)
1173 occ = args[1].data.sint;
1174 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001175
Christopher Faulet89dc4992019-04-17 12:02:59 +02001176 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001177
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001178 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001179 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001180
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001181 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1182 /* search for header from the beginning */
1183 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001184
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001185 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1186 /* no explicit occurrence and single fetch => last header by default */
1187 occ = -1;
1188
1189 if (!occ)
1190 /* prepare to report multiple occurrences for ACL fetches */
1191 smp->flags |= SMP_F_NOT_LAST;
1192
1193 smp->data.type = SMP_T_STR;
1194 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1195 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1196 return 1;
1197 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001198 smp->flags &= ~SMP_F_NOT_LAST;
1199 return 0;
1200}
1201
1202/* 6. Check on HTTP header count. The number of occurrences is returned.
1203 * Accepts exactly 1 argument of type string. It does not stop on commas and
1204 * returns full lines instead (useful for User-Agent or Date for example).
1205 */
1206static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1207{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001208 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
1209 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001210 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001211
Christopher Faulet46575cd2019-04-17 11:40:30 +02001212 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001213 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001214 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001215 struct http_hdr_ctx ctx;
1216 struct ist name;
1217
1218 if (!htx)
1219 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001220
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001221 if (args && args->type == ARGT_STR) {
1222 name.ptr = args->data.str.area;
1223 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001224 } else {
1225 name.ptr = NULL;
1226 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001227 }
1228
1229 ctx.blk = NULL;
1230 cnt = 0;
1231 while (http_find_header(htx, name, &ctx, 1))
1232 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001233 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001234 else {
1235 /* LEGACY version */
1236 struct hdr_idx *idx;
1237 struct hdr_ctx ctx;
1238 const struct http_msg *msg;
1239 const char *name = NULL;
1240 int len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001241
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001242 if (args && args->type == ARGT_STR) {
1243 name = args->data.str.area;
1244 len = args->data.str.data;
1245 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001246
Christopher Faulet89dc4992019-04-17 12:02:59 +02001247 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001248
1249 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001250 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001251
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001252 ctx.idx = 0;
1253 cnt = 0;
1254 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
1255 cnt++;
1256 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001257
1258 smp->data.type = SMP_T_SINT;
1259 smp->data.u.sint = cnt;
1260 smp->flags = SMP_F_VOL_HDR;
1261 return 1;
1262}
1263
1264static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
1265{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001266 /* possible keywords: req.hdr_names, res.hdr_names */
1267 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001268 struct buffer *temp;
1269 char del = ',';
1270
Christopher Faulet46575cd2019-04-17 11:40:30 +02001271 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001272 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001273 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001274 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001275
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001276 if (!htx)
1277 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001278
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001279 if (args && args->type == ARGT_STR)
1280 del = *args[0].data.str.area;
Willy Tarreau79e57332018-10-02 16:01:16 +02001281
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001282 temp = get_trash_chunk();
1283 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1284 struct htx_blk *blk = htx_get_blk(htx, pos);
1285 enum htx_blk_type type = htx_get_blk_type(blk);
1286 struct ist n;
Willy Tarreau79e57332018-10-02 16:01:16 +02001287
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001288 if (type == HTX_BLK_EOH)
1289 break;
1290 if (type != HTX_BLK_HDR)
1291 continue;
1292 n = htx_get_blk_name(htx, blk);
1293
1294 if (temp->data)
1295 temp->area[temp->data++] = del;
1296 chunk_memcat(temp, n.ptr, n.len);
1297 }
1298 }
1299 else {
1300 /* LEGACY version */
1301 struct hdr_idx *idx;
1302 struct hdr_ctx ctx;
1303 const struct http_msg *msg;
1304
1305 if (args && args->type == ARGT_STR)
1306 del = *args[0].data.str.area;
1307
Christopher Faulet89dc4992019-04-17 12:02:59 +02001308 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001309
1310 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001311 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001312
1313 temp = get_trash_chunk();
1314
1315 ctx.idx = 0;
1316 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
1317 if (temp->data)
1318 temp->area[temp->data++] = del;
1319 memcpy(temp->area + temp->data, ctx.line, ctx.del);
1320 temp->data += ctx.del;
1321 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001322 }
1323
1324 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001325 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001326 smp->flags = SMP_F_VOL_HDR;
1327 return 1;
1328}
1329
1330/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1331 * Accepts an optional argument of type string containing the header field name,
1332 * and an optional argument of type signed or unsigned integer to request an
1333 * explicit occurrence of the header. Note that in the event of a missing name,
1334 * headers are considered from the first one.
1335 */
1336static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1337{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001338 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
1339 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001340 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001341
Christopher Faulet46575cd2019-04-17 11:40:30 +02001342 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001343 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001344 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001345 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1346 struct ist name;
1347
1348 if (!ctx) {
1349 /* first call */
1350 ctx = &static_http_hdr_ctx;
1351 ctx->blk = NULL;
1352 smp->ctx.a[0] = ctx;
1353 }
1354
1355 if (args) {
1356 if (args[0].type != ARGT_STR)
1357 return 0;
1358 name.ptr = args[0].data.str.area;
1359 name.len = args[0].data.str.data;
1360
1361 if (args[1].type == ARGT_SINT)
1362 occ = args[1].data.sint;
1363 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001364
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001365 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001366 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001367
1368 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1369 /* search for header from the beginning */
1370 ctx->blk = NULL;
1371
1372 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1373 /* no explicit occurrence and single fetch => last header by default */
1374 occ = -1;
1375
1376 if (!occ)
1377 /* prepare to report multiple occurrences for ACL fetches */
1378 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001379
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001380 smp->data.type = SMP_T_STR;
1381 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1382 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1383 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001384 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001385 else {
1386 /* LEGACY version */
1387 struct hdr_idx *idx;
1388 struct hdr_ctx *ctx = smp->ctx.a[0];
1389 const struct http_msg *msg;
1390 const char *name_str = NULL;
1391 int name_len = 0;
1392
1393 if (!ctx) {
1394 /* first call */
1395 ctx = &static_hdr_ctx;
1396 ctx->idx = 0;
1397 smp->ctx.a[0] = ctx;
1398 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001399
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001400 if (args) {
1401 if (args[0].type != ARGT_STR)
1402 return 0;
1403 name_str = args[0].data.str.area;
1404 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001405
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001406 if (args[1].type == ARGT_SINT)
1407 occ = args[1].data.sint;
1408 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001409
Christopher Faulet89dc4992019-04-17 12:02:59 +02001410 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001411
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001412 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001413 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001414
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001415 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1416 /* search for header from the beginning */
1417 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001418
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001419 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1420 /* no explicit occurrence and single fetch => last header by default */
1421 occ = -1;
1422
1423 if (!occ)
1424 /* prepare to report multiple occurrences for ACL fetches */
1425 smp->flags |= SMP_F_NOT_LAST;
1426
1427 smp->data.type = SMP_T_STR;
1428 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1429 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1430 return 1;
1431 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001432
1433 smp->flags &= ~SMP_F_NOT_LAST;
1434 return 0;
1435}
1436
1437/* 6. Check on HTTP header count. The number of occurrences is returned.
1438 * Accepts exactly 1 argument of type string.
1439 */
1440static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1441{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001442 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
1443 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001444 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001445
Christopher Faulet46575cd2019-04-17 11:40:30 +02001446 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001447 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001448 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001449 struct http_hdr_ctx ctx;
1450 struct ist name;
1451
1452 if (!htx)
1453 return 0;
1454
1455 if (args && args->type == ARGT_STR) {
1456 name.ptr = args->data.str.area;
1457 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001458 } else {
1459 name.ptr = NULL;
1460 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001461 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001462
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001463 ctx.blk = NULL;
1464 cnt = 0;
1465 while (http_find_header(htx, name, &ctx, 0))
1466 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001467 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001468 else {
1469 /* LEGACY version */
1470 struct hdr_idx *idx;
1471 struct hdr_ctx ctx;
1472 const struct http_msg *msg;
1473 const char *name = NULL;
1474 int len = 0;
1475
1476 if (args && args->type == ARGT_STR) {
1477 name = args->data.str.area;
1478 len = args->data.str.data;
1479 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001480
Christopher Faulet89dc4992019-04-17 12:02:59 +02001481 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001482
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001483 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001484 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001485
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001486 ctx.idx = 0;
1487 cnt = 0;
1488 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
1489 cnt++;
1490 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001491
1492 smp->data.type = SMP_T_SINT;
1493 smp->data.u.sint = cnt;
1494 smp->flags = SMP_F_VOL_HDR;
1495 return 1;
1496}
1497
1498/* Fetch an HTTP header's integer value. The integer value is returned. It
1499 * takes a mandatory argument of type string and an optional one of type int
1500 * to designate a specific occurrence. It returns an unsigned integer, which
1501 * may or may not be appropriate for everything.
1502 */
1503static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1504{
1505 int ret = smp_fetch_hdr(args, smp, kw, private);
1506
1507 if (ret > 0) {
1508 smp->data.type = SMP_T_SINT;
1509 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1510 smp->data.u.str.data);
1511 }
1512
1513 return ret;
1514}
1515
1516/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1517 * and an optional one of type int to designate a specific occurrence.
1518 * It returns an IPv4 or IPv6 address.
1519 */
1520static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1521{
1522 int ret;
1523
1524 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
1525 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
1526 smp->data.type = SMP_T_IPV4;
1527 break;
1528 } else {
1529 struct buffer *temp = get_trash_chunk();
1530 if (smp->data.u.str.data < temp->size - 1) {
1531 memcpy(temp->area, smp->data.u.str.area,
1532 smp->data.u.str.data);
1533 temp->area[smp->data.u.str.data] = '\0';
1534 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1535 smp->data.type = SMP_T_IPV6;
1536 break;
1537 }
1538 }
1539 }
1540
1541 /* if the header doesn't match an IP address, fetch next one */
1542 if (!(smp->flags & SMP_F_NOT_LAST))
1543 return 0;
1544 }
1545 return ret;
1546}
1547
1548/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1549 * the first '/' after the possible hostname, and ends before the possible '?'.
1550 */
1551static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1552{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001553 struct channel *chn = SMP_REQ_CHN(smp);
1554
Christopher Faulet46575cd2019-04-17 11:40:30 +02001555 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001556 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001557 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001558 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001559 struct ist path;
1560 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001561
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001562 if (!htx)
1563 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001564
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001565 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001566 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001567 if (!path.ptr)
1568 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001569
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001570 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001571 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001572
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001573 /* OK, we got the '/' ! */
1574 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001575 smp->data.u.str.area = path.ptr;
1576 smp->data.u.str.data = len;
1577 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1578 }
1579 else {
1580 struct http_txn *txn;
1581 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001582
Christopher Faulet89dc4992019-04-17 12:02:59 +02001583 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001584
1585 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001586 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001587 ptr = http_txn_get_path(txn);
1588 if (!ptr)
1589 return 0;
1590
1591 /* OK, we got the '/' ! */
1592 smp->data.type = SMP_T_STR;
1593 smp->data.u.str.area = ptr;
1594
1595 while (ptr < end && *ptr != '?')
1596 ptr++;
1597
1598 smp->data.u.str.data = ptr - smp->data.u.str.area;
1599 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1600 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001601 return 1;
1602}
1603
1604/* This produces a concatenation of the first occurrence of the Host header
1605 * followed by the path component if it begins with a slash ('/'). This means
1606 * that '*' will not be added, resulting in exactly the first Host entry.
1607 * If no Host header is found, then the path is returned as-is. The returned
1608 * value is stored in the trash so it does not need to be marked constant.
1609 * The returned sample is of type string.
1610 */
1611static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1612{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001613 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001614 struct buffer *temp;
1615
Christopher Faulet46575cd2019-04-17 11:40:30 +02001616 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001617 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001618 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001619 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001620 struct http_hdr_ctx ctx;
1621 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001622
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001623 if (!htx)
1624 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001625
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001626 ctx.blk = NULL;
1627 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1628 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001629
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001630 /* OK we have the header value in ctx.value */
1631 temp = get_trash_chunk();
1632 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1633
1634 /* now retrieve the path */
1635 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001636 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001637 if (path.ptr) {
1638 size_t len;
1639
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001640 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1641 ;
1642
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001643 if (len && *(path.ptr) == '/')
1644 chunk_memcat(temp, path.ptr, len);
1645 }
1646
1647 smp->data.type = SMP_T_STR;
1648 smp->data.u.str = *temp;
1649 }
1650 else {
1651 /* LEGACY version */
1652 struct http_txn *txn;
1653 char *ptr, *end, *beg;
1654 struct hdr_ctx ctx;
1655
Christopher Faulet89dc4992019-04-17 12:02:59 +02001656 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001657
1658 txn = smp->strm->txn;
1659 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001660 if (!http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001661 return smp_fetch_path(args, smp, kw, private);
1662
1663 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1664 temp = get_trash_chunk();
1665 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1666 smp->data.type = SMP_T_STR;
1667 smp->data.u.str.area = temp->area;
1668 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001669
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001670 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001671 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001672 beg = http_txn_get_path(txn);
1673 if (!beg)
1674 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001675
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001676 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1677
1678 if (beg < ptr && *beg == '/') {
1679 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1680 ptr - beg);
1681 smp->data.u.str.data += ptr - beg;
1682 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001683 }
1684
1685 smp->flags = SMP_F_VOL_1ST;
1686 return 1;
1687}
1688
1689/* This produces a 32-bit hash of the concatenation of the first occurrence of
1690 * the Host header followed by the path component if it begins with a slash ('/').
1691 * This means that '*' will not be added, resulting in exactly the first Host
1692 * entry. If no Host header is found, then the path is used. The resulting value
1693 * is hashed using the path hash followed by a full avalanche hash and provides a
1694 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1695 * high-traffic sites without having to store whole paths.
1696 */
1697static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1698{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001699 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001700 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001701
Christopher Faulet46575cd2019-04-17 11:40:30 +02001702 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001703 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001704 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001705 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001706 struct http_hdr_ctx ctx;
1707 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001708
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001709 if (!htx)
1710 return 0;
1711
1712 ctx.blk = NULL;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001713 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001714 /* OK we have the header value in ctx.value */
1715 while (ctx.value.len--)
1716 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1717 }
1718
1719 /* now retrieve the path */
1720 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001721 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001722 if (path.ptr) {
1723 size_t len;
1724
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001725 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1726 ;
1727
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001728 if (len && *(path.ptr) == '/') {
1729 while (len--)
1730 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1731 }
1732 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001733 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001734 else {
1735 /* LEGACY version */
1736 struct http_txn *txn;
1737 struct hdr_ctx ctx;
1738 char *ptr, *beg, *end;
1739 int len;
1740
Christopher Faulet89dc4992019-04-17 12:02:59 +02001741 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001742
1743 txn = smp->strm->txn;
1744 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001745 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001746 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1747 ptr = ctx.line + ctx.val;
1748 len = ctx.vlen;
1749 while (len--)
1750 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1751 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001752
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001753 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001754 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001755 beg = http_txn_get_path(txn);
1756 if (!beg)
1757 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001758
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001759 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001760
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001761 if (beg < ptr && *beg == '/') {
1762 while (beg < ptr)
1763 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
1764 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001765 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001766
Willy Tarreau79e57332018-10-02 16:01:16 +02001767 hash = full_hash(hash);
1768
1769 smp->data.type = SMP_T_SINT;
1770 smp->data.u.sint = hash;
1771 smp->flags = SMP_F_VOL_1ST;
1772 return 1;
1773}
1774
1775/* This concatenates the source address with the 32-bit hash of the Host and
1776 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1777 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1778 * on the source address length. The path hash is stored before the address so
1779 * that in environments where IPv6 is insignificant, truncating the output to
1780 * 8 bytes would still work.
1781 */
1782static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1783{
1784 struct buffer *temp;
1785 struct connection *cli_conn = objt_conn(smp->sess->origin);
1786
1787 if (!cli_conn)
1788 return 0;
1789
1790 if (!smp_fetch_base32(args, smp, kw, private))
1791 return 0;
1792
1793 temp = get_trash_chunk();
1794 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1795 temp->data += sizeof(unsigned int);
1796
1797 switch (cli_conn->addr.from.ss_family) {
1798 case AF_INET:
1799 memcpy(temp->area + temp->data,
1800 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1801 4);
1802 temp->data += 4;
1803 break;
1804 case AF_INET6:
1805 memcpy(temp->area + temp->data,
1806 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1807 16);
1808 temp->data += 16;
1809 break;
1810 default:
1811 return 0;
1812 }
1813
1814 smp->data.u.str = *temp;
1815 smp->data.type = SMP_T_BIN;
1816 return 1;
1817}
1818
1819/* Extracts the query string, which comes after the question mark '?'. If no
1820 * question mark is found, nothing is returned. Otherwise it returns a sample
1821 * of type string carrying the whole query string.
1822 */
1823static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1824{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001825 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001826 char *ptr, *end;
1827
Christopher Faulet46575cd2019-04-17 11:40:30 +02001828 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001829 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001830 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001831 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001832
1833 if (!htx)
1834 return 0;
1835
1836 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001837 ptr = HTX_SL_REQ_UPTR(sl);
1838 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001839 }
1840 else {
1841 /* LEGACY version */
1842 struct http_txn *txn;
1843
Christopher Faulet89dc4992019-04-17 12:02:59 +02001844 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001845
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001846 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001847 ptr = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001848 end = ptr + txn->req.sl.rq.u_l;
1849 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001850
1851 /* look up the '?' */
1852 do {
1853 if (ptr == end)
1854 return 0;
1855 } while (*ptr++ != '?');
1856
1857 smp->data.type = SMP_T_STR;
1858 smp->data.u.str.area = ptr;
1859 smp->data.u.str.data = end - ptr;
1860 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1861 return 1;
1862}
1863
1864static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1865{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001866 struct channel *chn = SMP_REQ_CHN(smp);
1867
Christopher Faulet46575cd2019-04-17 11:40:30 +02001868 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001869 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001870 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001871
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001872 if (!htx)
1873 return 0;
1874 }
1875 else {
1876 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02001877
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001878 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
1879 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
1880 */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001881 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001882 }
1883 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001884 smp->data.u.sint = 1;
1885 return 1;
1886}
1887
1888/* return a valid test if the current request is the first one on the connection */
1889static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1890{
1891 smp->data.type = SMP_T_BOOL;
1892 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1893 return 1;
1894}
1895
1896/* Accepts exactly 1 argument of type userlist */
1897static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1898{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001899 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001900
1901 if (!args || args->type != ARGT_USR)
1902 return 0;
1903
Christopher Faulet46575cd2019-04-17 11:40:30 +02001904 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001905 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001906 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001907
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001908 if (!htx)
1909 return 0;
1910 }
1911 else {
1912 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001913 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001914 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001915
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001916 if (!get_http_auth(smp))
1917 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001918 smp->data.type = SMP_T_BOOL;
1919 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001920 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001921 return 1;
1922}
1923
1924/* Accepts exactly 1 argument of type userlist */
1925static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1926{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001927 struct channel *chn = SMP_REQ_CHN(smp);
1928
Willy Tarreau79e57332018-10-02 16:01:16 +02001929 if (!args || args->type != ARGT_USR)
1930 return 0;
1931
Christopher Faulet46575cd2019-04-17 11:40:30 +02001932 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001933 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001934 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001935
1936 if (!htx)
1937 return 0;
1938 }
1939 else {
1940 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001941 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001942 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001943
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001944 if (!get_http_auth(smp))
Willy Tarreau79e57332018-10-02 16:01:16 +02001945 return 0;
1946
1947 /* if the user does not belong to the userlist or has a wrong password,
1948 * report that it unconditionally does not match. Otherwise we return
1949 * a string containing the username.
1950 */
1951 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1952 smp->strm->txn->auth.pass))
1953 return 0;
1954
1955 /* pat_match_auth() will need the user list */
1956 smp->ctx.a[0] = args->data.usr;
1957
1958 smp->data.type = SMP_T_STR;
1959 smp->flags = SMP_F_CONST;
1960 smp->data.u.str.area = smp->strm->txn->auth.user;
1961 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1962
1963 return 1;
1964}
1965
1966/* Fetch a captured HTTP request header. The index is the position of
1967 * the "capture" option in the configuration file
1968 */
1969static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1970{
1971 struct proxy *fe = strm_fe(smp->strm);
1972 int idx;
1973
1974 if (!args || args->type != ARGT_SINT)
1975 return 0;
1976
1977 idx = args->data.sint;
1978
1979 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1980 return 0;
1981
1982 smp->data.type = SMP_T_STR;
1983 smp->flags |= SMP_F_CONST;
1984 smp->data.u.str.area = smp->strm->req_cap[idx];
1985 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1986
1987 return 1;
1988}
1989
1990/* Fetch a captured HTTP response header. The index is the position of
1991 * the "capture" option in the configuration file
1992 */
1993static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1994{
1995 struct proxy *fe = strm_fe(smp->strm);
1996 int idx;
1997
1998 if (!args || args->type != ARGT_SINT)
1999 return 0;
2000
2001 idx = args->data.sint;
2002
2003 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
2004 return 0;
2005
2006 smp->data.type = SMP_T_STR;
2007 smp->flags |= SMP_F_CONST;
2008 smp->data.u.str.area = smp->strm->res_cap[idx];
2009 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
2010
2011 return 1;
2012}
2013
2014/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
2015static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
2016{
2017 struct buffer *temp;
2018 struct http_txn *txn = smp->strm->txn;
2019 char *ptr;
2020
2021 if (!txn || !txn->uri)
2022 return 0;
2023
2024 ptr = txn->uri;
2025
2026 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2027 ptr++;
2028
2029 temp = get_trash_chunk();
2030 temp->area = txn->uri;
2031 temp->data = ptr - txn->uri;
2032 smp->data.u.str = *temp;
2033 smp->data.type = SMP_T_STR;
2034 smp->flags = SMP_F_CONST;
2035
2036 return 1;
2037
2038}
2039
2040/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
2041static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
2042{
2043 struct http_txn *txn = smp->strm->txn;
2044 struct ist path;
2045 const char *ptr;
2046
2047 if (!txn || !txn->uri)
2048 return 0;
2049
2050 ptr = txn->uri;
2051
2052 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2053 ptr++;
2054
2055 if (!*ptr)
2056 return 0;
2057
Christopher Faulet78337bb2018-11-15 14:35:18 +01002058 /* skip the first space and find space after URI */
2059 path = ist2(++ptr, 0);
2060 while (*ptr != ' ' && *ptr != '\0')
2061 ptr++;
2062 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002063
Christopher Faulet78337bb2018-11-15 14:35:18 +01002064 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02002065 if (!path.ptr)
2066 return 0;
2067
2068 smp->data.u.str.area = path.ptr;
2069 smp->data.u.str.data = path.len;
2070 smp->data.type = SMP_T_STR;
2071 smp->flags = SMP_F_CONST;
2072
2073 return 1;
2074}
2075
2076/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
2077 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2078 */
2079static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2080{
2081 struct http_txn *txn = smp->strm->txn;
2082
2083 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2084 return 0;
2085
2086 if (txn->req.flags & HTTP_MSGF_VER_11)
2087 smp->data.u.str.area = "HTTP/1.1";
2088 else
2089 smp->data.u.str.area = "HTTP/1.0";
2090
2091 smp->data.u.str.data = 8;
2092 smp->data.type = SMP_T_STR;
2093 smp->flags = SMP_F_CONST;
2094 return 1;
2095
2096}
2097
2098/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2099 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2100 */
2101static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2102{
2103 struct http_txn *txn = smp->strm->txn;
2104
2105 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2106 return 0;
2107
2108 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2109 smp->data.u.str.area = "HTTP/1.1";
2110 else
2111 smp->data.u.str.area = "HTTP/1.0";
2112
2113 smp->data.u.str.data = 8;
2114 smp->data.type = SMP_T_STR;
2115 smp->flags = SMP_F_CONST;
2116 return 1;
2117
2118}
2119
2120/* Iterate over all cookies present in a message. The context is stored in
2121 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2122 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2123 * the direction, multiple cookies may be parsed on the same line or not.
2124 * The cookie name is in args and the name length in args->data.str.len.
2125 * Accepts exactly 1 argument of type string. If the input options indicate
2126 * that no iterating is desired, then only last value is fetched if any.
2127 * The returned sample is of type CSTR. Can be used to parse cookies in other
2128 * files.
2129 */
2130static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2131{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002132 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
2133 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002134 int occ = 0;
2135 int found = 0;
2136
2137 if (!args || args->type != ARGT_STR)
2138 return 0;
2139
Christopher Faulet46575cd2019-04-17 11:40:30 +02002140 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002141 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002142 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002143 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2144 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002145
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002146 if (!ctx) {
2147 /* first call */
2148 ctx = &static_http_hdr_ctx;
2149 ctx->blk = NULL;
2150 smp->ctx.a[2] = ctx;
2151 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002152
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002153 if (!htx)
2154 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002155
Christopher Faulet89dc4992019-04-17 12:02:59 +02002156 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002157
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002158 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2159 /* no explicit occurrence and single fetch => last cookie by default */
2160 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02002161
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002162 /* OK so basically here, either we want only one value and it's the
2163 * last one, or we want to iterate over all of them and we fetch the
2164 * next one.
Willy Tarreau79e57332018-10-02 16:01:16 +02002165 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002166
2167 if (!(smp->flags & SMP_F_NOT_LAST)) {
2168 /* search for the header from the beginning, we must first initialize
2169 * the search parameters.
2170 */
2171 smp->ctx.a[0] = NULL;
2172 ctx->blk = NULL;
2173 }
2174
2175 smp->flags |= SMP_F_VOL_HDR;
2176 while (1) {
2177 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2178 if (!smp->ctx.a[0]) {
2179 if (!http_find_header(htx, hdr, ctx, 0))
2180 goto out;
2181
2182 if (ctx->value.len < args->data.str.data + 1)
2183 continue;
2184
2185 smp->ctx.a[0] = ctx->value.ptr;
2186 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2187 }
2188
2189 smp->data.type = SMP_T_STR;
2190 smp->flags |= SMP_F_CONST;
2191 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2192 args->data.str.area, args->data.str.data,
2193 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2194 &smp->data.u.str.area,
2195 &smp->data.u.str.data);
2196 if (smp->ctx.a[0]) {
2197 found = 1;
2198 if (occ >= 0) {
2199 /* one value was returned into smp->data.u.str.{str,len} */
2200 smp->flags |= SMP_F_NOT_LAST;
2201 return 1;
2202 }
2203 }
2204 /* if we're looking for last occurrence, let's loop */
2205 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002206 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002207 else {
2208 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002209 struct hdr_idx *idx;
2210 struct hdr_ctx *ctx = smp->ctx.a[2];
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002211 const char *hdr_name;
2212 int hdr_name_len;
2213 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002214
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002215 if (!ctx) {
2216 /* first call */
2217 ctx = &static_hdr_ctx;
2218 ctx->idx = 0;
2219 smp->ctx.a[2] = ctx;
2220 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002221
Christopher Faulet89dc4992019-04-17 12:02:59 +02002222 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002223
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002224 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002225 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002226 hdr_name = "Cookie";
2227 hdr_name_len = 6;
2228 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002229 hdr_name = "Set-Cookie";
2230 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002231 }
2232
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002233 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2234 /* no explicit occurrence and single fetch => last cookie by default */
2235 occ = -1;
2236
2237 /* OK so basically here, either we want only one value and it's the
2238 * last one, or we want to iterate over all of them and we fetch the
2239 * next one.
2240 */
2241
Christopher Faulet89dc4992019-04-17 12:02:59 +02002242 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002243 if (!(smp->flags & SMP_F_NOT_LAST)) {
2244 /* search for the header from the beginning, we must first initialize
2245 * the search parameters.
2246 */
2247 smp->ctx.a[0] = NULL;
2248 ctx->idx = 0;
2249 }
2250
2251 smp->flags |= SMP_F_VOL_HDR;
2252
2253 while (1) {
2254 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2255 if (!smp->ctx.a[0]) {
2256 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2257 goto out;
2258
2259 if (ctx->vlen < args->data.str.data + 1)
2260 continue;
2261
2262 smp->ctx.a[0] = ctx->line + ctx->val;
2263 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2264 }
2265
2266 smp->data.type = SMP_T_STR;
2267 smp->flags |= SMP_F_CONST;
2268 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2269 args->data.str.area, args->data.str.data,
2270 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2271 &smp->data.u.str.area, &smp->data.u.str.data);
2272 if (smp->ctx.a[0]) {
2273 found = 1;
2274 if (occ >= 0) {
2275 /* one value was returned into smp->data.u.str.{str,len} */
2276 smp->flags |= SMP_F_NOT_LAST;
2277 return 1;
2278 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002279 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002280 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002281 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002282 }
2283 /* all cookie headers and values were scanned. If we're looking for the
2284 * last occurrence, we may return it now.
2285 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002286 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002287 smp->flags &= ~SMP_F_NOT_LAST;
2288 return found;
2289}
2290
2291/* Iterate over all cookies present in a request to count how many occurrences
2292 * match the name in args and args->data.str.len. If <multi> is non-null, then
2293 * multiple cookies may be parsed on the same line. The returned sample is of
2294 * type UINT. Accepts exactly 1 argument of type string.
2295 */
2296static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2297{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002298 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
2299 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002300 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002301 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002302
2303 if (!args || args->type != ARGT_STR)
2304 return 0;
2305
Christopher Faulet46575cd2019-04-17 11:40:30 +02002306 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002307 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002308 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002309 struct http_hdr_ctx ctx;
2310 struct ist hdr;
2311
2312 if (!htx)
2313 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002314
Christopher Faulet89dc4992019-04-17 12:02:59 +02002315 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002316
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002317 val_end = val_beg = NULL;
2318 ctx.blk = NULL;
2319 cnt = 0;
2320 while (1) {
2321 /* Note: val_beg == NULL every time we need to fetch a new header */
2322 if (!val_beg) {
2323 if (!http_find_header(htx, hdr, &ctx, 0))
2324 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002325
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002326 if (ctx.value.len < args->data.str.data + 1)
2327 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002328
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002329 val_beg = ctx.value.ptr;
2330 val_end = val_beg + ctx.value.len;
2331 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002332
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002333 smp->data.type = SMP_T_STR;
2334 smp->flags |= SMP_F_CONST;
2335 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2336 args->data.str.area, args->data.str.data,
2337 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2338 &smp->data.u.str.area,
2339 &smp->data.u.str.data))) {
2340 cnt++;
2341 }
2342 }
2343 }
2344 else {
2345 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002346 struct hdr_idx *idx;
2347 struct hdr_ctx ctx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002348 const char *hdr_name;
2349 int hdr_name_len;
2350 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002351
Christopher Faulet89dc4992019-04-17 12:02:59 +02002352 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002353
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002354 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002355 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002356 hdr_name = "Cookie";
2357 hdr_name_len = 6;
2358 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002359 hdr_name = "Set-Cookie";
2360 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002361 }
2362
Christopher Faulet89dc4992019-04-17 12:02:59 +02002363 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002364 val_end = val_beg = NULL;
2365 ctx.idx = 0;
2366 cnt = 0;
2367
2368 while (1) {
2369 /* Note: val_beg == NULL every time we need to fetch a new header */
2370 if (!val_beg) {
2371 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2372 break;
2373
2374 if (ctx.vlen < args->data.str.data + 1)
2375 continue;
2376
2377 val_beg = ctx.line + ctx.val;
2378 val_end = val_beg + ctx.vlen;
2379 }
2380
2381 smp->data.type = SMP_T_STR;
2382 smp->flags |= SMP_F_CONST;
2383 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2384 args->data.str.area, args->data.str.data,
2385 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2386 &smp->data.u.str.area, &smp->data.u.str.data))) {
2387 cnt++;
2388 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002389 }
2390 }
2391
2392 smp->data.type = SMP_T_SINT;
2393 smp->data.u.sint = cnt;
2394 smp->flags |= SMP_F_VOL_HDR;
2395 return 1;
2396}
2397
2398/* Fetch an cookie's integer value. The integer value is returned. It
2399 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2400 */
2401static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2402{
2403 int ret = smp_fetch_cookie(args, smp, kw, private);
2404
2405 if (ret > 0) {
2406 smp->data.type = SMP_T_SINT;
2407 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2408 smp->data.u.str.data);
2409 }
2410
2411 return ret;
2412}
2413
2414/************************************************************************/
2415/* The code below is dedicated to sample fetches */
2416/************************************************************************/
2417
2418/* This scans a URL-encoded query string. It takes an optionally wrapping
2419 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2420 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2421 * pointers are updated for next iteration before leaving.
2422 */
2423static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2424{
2425 const char *vstart, *vend;
2426 struct buffer *temp;
2427 const char **chunks = (const char **)smp->ctx.a;
2428
2429 if (!http_find_next_url_param(chunks, name, name_len,
2430 &vstart, &vend, delim))
2431 return 0;
2432
2433 /* Create sample. If the value is contiguous, return the pointer as CONST,
2434 * if the value is wrapped, copy-it in a buffer.
2435 */
2436 smp->data.type = SMP_T_STR;
2437 if (chunks[2] &&
2438 vstart >= chunks[0] && vstart <= chunks[1] &&
2439 vend >= chunks[2] && vend <= chunks[3]) {
2440 /* Wrapped case. */
2441 temp = get_trash_chunk();
2442 memcpy(temp->area, vstart, chunks[1] - vstart);
2443 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2444 vend - chunks[2]);
2445 smp->data.u.str.area = temp->area;
2446 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2447 } else {
2448 /* Contiguous case. */
2449 smp->data.u.str.area = (char *)vstart;
2450 smp->data.u.str.data = vend - vstart;
2451 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2452 }
2453
2454 /* Update context, check wrapping. */
2455 chunks[0] = vend;
2456 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2457 chunks[1] = chunks[3];
2458 chunks[2] = NULL;
2459 }
2460
2461 if (chunks[0] < chunks[1])
2462 smp->flags |= SMP_F_NOT_LAST;
2463
2464 return 1;
2465}
2466
2467/* This function iterates over each parameter of the query string. It uses
2468 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2469 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2470 * An optional parameter name is passed in args[0], otherwise any parameter is
2471 * considered. It supports an optional delimiter argument for the beginning of
2472 * the string in args[1], which defaults to "?".
2473 */
2474static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2475{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002476 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002477 char delim = '?';
2478 const char *name;
2479 int name_len;
2480
2481 if (!args ||
2482 (args[0].type && args[0].type != ARGT_STR) ||
2483 (args[1].type && args[1].type != ARGT_STR))
2484 return 0;
2485
2486 name = "";
2487 name_len = 0;
2488 if (args->type == ARGT_STR) {
2489 name = args->data.str.area;
2490 name_len = args->data.str.data;
2491 }
2492
2493 if (args[1].type)
2494 delim = *args[1].data.str.area;
2495
2496 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002497 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002498 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002499 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002500 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002501
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002502 if (!htx)
2503 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002504
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002505 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002506 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 +02002507 if (!smp->ctx.a[0])
2508 return 0;
2509
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002510 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002511 }
2512 else {
2513 /* LEGACY version */
2514 struct http_msg *msg;
2515
Christopher Faulet89dc4992019-04-17 12:02:59 +02002516 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002517
2518 msg = &smp->strm->txn->req;
2519
Christopher Faulet89dc4992019-04-17 12:02:59 +02002520 smp->ctx.a[0] = http_find_param_list(ci_head(chn) + msg->sl.rq.u,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002521 msg->sl.rq.u_l, delim);
2522 if (!smp->ctx.a[0])
2523 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002524
Christopher Faulet89dc4992019-04-17 12:02:59 +02002525 smp->ctx.a[1] = ci_head(chn) + msg->sl.rq.u + msg->sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002526 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002527
2528 /* Assume that the context is filled with NULL pointer
2529 * before the first call.
2530 * smp->ctx.a[2] = NULL;
2531 * smp->ctx.a[3] = NULL;
2532 */
2533 }
2534
2535 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2536}
2537
2538/* This function iterates over each parameter of the body. This requires
2539 * that the body has been waited for using http-buffer-request. It uses
2540 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2541 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2542 * optional second part if the body wraps at the end of the buffer. An optional
2543 * parameter name is passed in args[0], otherwise any parameter is considered.
2544 */
2545static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2546{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002547 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002548 const char *name;
2549 int name_len;
2550
2551 if (!args || (args[0].type && args[0].type != ARGT_STR))
2552 return 0;
2553
2554 name = "";
2555 name_len = 0;
2556 if (args[0].type == ARGT_STR) {
2557 name = args[0].data.str.area;
2558 name_len = args[0].data.str.data;
2559 }
2560
2561 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002562 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002563 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002564 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002565 struct buffer *temp;
2566 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002567
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002568 if (!htx)
2569 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002570
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002571 temp = get_trash_chunk();
2572 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
2573 struct htx_blk *blk = htx_get_blk(htx, pos);
2574 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002575
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002576 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
2577 break;
2578 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01002579 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002580 return 0;
2581 }
2582 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002583
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002584 smp->ctx.a[0] = temp->area;
2585 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002586
2587 /* Assume that the context is filled with NULL pointer
2588 * before the first call.
2589 * smp->ctx.a[2] = NULL;
2590 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002591 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002592 }
2593 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002594 /* LEGACY version */
2595 struct http_msg *msg;
2596 unsigned long len;
2597 unsigned long block1;
2598 char *body;
2599
Christopher Faulet89dc4992019-04-17 12:02:59 +02002600 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002601
Christopher Faulet89dc4992019-04-17 12:02:59 +02002602 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002603 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +02002604 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002605
2606 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002607 if (block1 > b_wrap(&chn->buf) - body)
2608 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002609
2610 if (block1 == len) {
2611 /* buffer is not wrapped (or empty) */
2612 smp->ctx.a[0] = body;
2613 smp->ctx.a[1] = body + len;
2614
2615 /* Assume that the context is filled with NULL pointer
2616 * before the first call.
2617 * smp->ctx.a[2] = NULL;
2618 * smp->ctx.a[3] = NULL;
2619 */
2620 }
2621 else {
2622 /* buffer is wrapped, we need to defragment it */
2623 smp->ctx.a[0] = body;
2624 smp->ctx.a[1] = body + block1;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002625 smp->ctx.a[2] = b_orig(&chn->buf);
2626 smp->ctx.a[3] = b_orig(&chn->buf) + ( len - block1 );
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002627 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002628 }
2629 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002630
Willy Tarreau79e57332018-10-02 16:01:16 +02002631 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2632}
2633
2634/* Return the signed integer value for the specified url parameter (see url_param
2635 * above).
2636 */
2637static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2638{
2639 int ret = smp_fetch_url_param(args, smp, kw, private);
2640
2641 if (ret > 0) {
2642 smp->data.type = SMP_T_SINT;
2643 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2644 smp->data.u.str.data);
2645 }
2646
2647 return ret;
2648}
2649
2650/* This produces a 32-bit hash of the concatenation of the first occurrence of
2651 * the Host header followed by the path component if it begins with a slash ('/').
2652 * This means that '*' will not be added, resulting in exactly the first Host
2653 * entry. If no Host header is found, then the path is used. The resulting value
2654 * is hashed using the url hash followed by a full avalanche hash and provides a
2655 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2656 * high-traffic sites without having to store whole paths.
2657 * this differs from the base32 functions in that it includes the url parameters
2658 * as well as the path
2659 */
2660static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2661{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002662 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002663 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002664
Christopher Faulet46575cd2019-04-17 11:40:30 +02002665 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002666 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002667 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002668 struct http_hdr_ctx ctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002669 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002670 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002671
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002672 if (!htx)
2673 return 0;
2674
2675 ctx.blk = NULL;
2676 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2677 /* OK we have the header value in ctx.value */
2678 while (ctx.value.len--)
2679 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2680 }
2681
2682 /* now retrieve the path */
2683 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002684 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002685 while (path.len > 0 && *(path.ptr) != '?') {
2686 path.ptr++;
2687 path.len--;
2688 }
2689 if (path.len && *(path.ptr) == '/') {
2690 while (path.len--)
2691 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2692 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002693 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002694 else {
2695 /* LEGACY version */
2696 struct http_txn *txn;
2697 struct hdr_ctx ctx;
2698 char *ptr, *beg, *end;
2699 int len;
2700
Christopher Faulet89dc4992019-04-17 12:02:59 +02002701 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002702
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002703 txn = smp->strm->txn;
2704 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002705 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002706 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2707 ptr = ctx.line + ctx.val;
2708 len = ctx.vlen;
2709 while (len--)
2710 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2711 }
2712
2713 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02002714 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002715 beg = http_txn_get_path(txn);
2716 if (!beg)
2717 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002718
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002719 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002720
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002721 if (beg < ptr && *beg == '/') {
2722 while (beg < ptr)
2723 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2724 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002725 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002726
Willy Tarreau79e57332018-10-02 16:01:16 +02002727 hash = full_hash(hash);
2728
2729 smp->data.type = SMP_T_SINT;
2730 smp->data.u.sint = hash;
2731 smp->flags = SMP_F_VOL_1ST;
2732 return 1;
2733}
2734
2735/* This concatenates the source address with the 32-bit hash of the Host and
2736 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2737 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2738 * on the source address length. The URL hash is stored before the address so
2739 * that in environments where IPv6 is insignificant, truncating the output to
2740 * 8 bytes would still work.
2741 */
2742static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2743{
2744 struct buffer *temp;
2745 struct connection *cli_conn = objt_conn(smp->sess->origin);
2746
2747 if (!cli_conn)
2748 return 0;
2749
2750 if (!smp_fetch_url32(args, smp, kw, private))
2751 return 0;
2752
2753 temp = get_trash_chunk();
2754 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2755 temp->data += sizeof(unsigned int);
2756
2757 switch (cli_conn->addr.from.ss_family) {
2758 case AF_INET:
2759 memcpy(temp->area + temp->data,
2760 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2761 4);
2762 temp->data += 4;
2763 break;
2764 case AF_INET6:
2765 memcpy(temp->area + temp->data,
2766 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2767 16);
2768 temp->data += 16;
2769 break;
2770 default:
2771 return 0;
2772 }
2773
2774 smp->data.u.str = *temp;
2775 smp->data.type = SMP_T_BIN;
2776 return 1;
2777}
2778
2779/************************************************************************/
2780/* Other utility functions */
2781/************************************************************************/
2782
2783/* This function is used to validate the arguments passed to any "hdr" fetch
2784 * keyword. These keywords support an optional positive or negative occurrence
2785 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2786 * is assumed that the types are already the correct ones. Returns 0 on error,
2787 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2788 * error message in case of error, that the caller is responsible for freeing.
2789 * The initial location must either be freeable or NULL.
2790 * Note: this function's pointer is checked from Lua.
2791 */
2792int val_hdr(struct arg *arg, char **err_msg)
2793{
2794 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2795 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2796 return 0;
2797 }
2798 return 1;
2799}
2800
2801/************************************************************************/
2802/* All supported sample fetch keywords must be declared here. */
2803/************************************************************************/
2804
2805/* Note: must not be declared <const> as its list will be overwritten */
2806static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2807 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2808 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2809 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2810
2811 /* capture are allocated and are permanent in the stream */
2812 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2813
2814 /* retrieve these captures from the HTTP logs */
2815 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2816 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2817 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2818
2819 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2820 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2821
2822 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2823 * are only here to match the ACL's name, are request-only and are used
2824 * for ACL compatibility only.
2825 */
2826 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2827 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
2828 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2829 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2830
2831 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2832 * only here to match the ACL's name, are request-only and are used for
2833 * ACL compatibility only.
2834 */
2835 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
2836 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2837 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2838 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2839
2840 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2841 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2842 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2843 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2844 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2845 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2846
2847 /* HTTP protocol on the request path */
2848 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2849 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2850
2851 /* HTTP version on the request path */
2852 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2853 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2854
2855 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2856 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2857 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2858 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2859
2860 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2861 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2862
2863 /* HTTP version on the response path */
2864 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2865 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2866
2867 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2868 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2869 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2870 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2871
2872 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2873 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2874 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2875 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2876 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2877 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2878 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2879
2880 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2881 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2882 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2883 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2884
2885 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2886 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2887 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2888 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2889 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2890 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2891 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2892
2893 /* scook is valid only on the response and is used for ACL compatibility */
2894 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2895 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2896 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2897 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2898
2899 /* shdr is valid only on the response and is used for ACL compatibility */
2900 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2901 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2902 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2903 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2904
2905 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2906 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2907 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2908 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2909 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2910 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2911 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2912 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2913 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2914 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2915 { /* END */ },
2916}};
2917
Willy Tarreau0108d902018-11-25 19:14:37 +01002918INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002919
2920/*
2921 * Local variables:
2922 * c-indent-level: 8
2923 * c-basic-offset: 8
2924 * End:
2925 */