blob: 2d3a381c48433952d5f404be075917903d376976 [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;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200273 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200274 }
275
276 /* OK we just got a valid HTTP message. If not already done by
277 * HTTP analyzers, we have some minor preparation to perform so
278 * that further checks can rely on HTTP tests.
279 */
280 if (sl && msg->msg_state < HTTP_MSG_BODY) {
281 if (!(chn->flags & CF_ISRESP)) {
282 txn->meth = sl->info.req.meth;
283 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
284 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200285 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200286 else
287 txn->status = sl->info.res.status;
288 if (sl->flags & HTX_SL_F_VER_11)
289 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200290 }
291
292 /* everything's OK */
293 smp->data.u.sint = 1;
294 return htx;
295}
296
297/* This function ensures that the prerequisites for an L7 fetch are ready,
298 * which means that a request or response is ready. If some data is missing,
299 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau79e57332018-10-02 16:01:16 +0200300 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
301 * another test is made to ensure the required information is not gone.
302 *
303 * The function returns :
304 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
305 * decide whether or not an HTTP message is present ;
306 * 0 if the requested data cannot be fetched or if it is certain that
307 * we'll never have any HTTP message there ;
308 * 1 if an HTTP message is ready
309 */
310int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Christopher Faulet89dc4992019-04-17 12:02:59 +0200311 struct channel *chn, struct sample *smp, int req_vol)
Willy Tarreau79e57332018-10-02 16:01:16 +0200312{
313 struct http_txn *txn;
314 struct http_msg *msg;
315
316 /* Note: it is possible that <s> is NULL when called before stream
317 * initialization (eg: tcp-request connection), so this function is the
318 * one responsible for guarding against this case for all HTTP users.
319 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200320 if (!s || !chn)
Willy Tarreau79e57332018-10-02 16:01:16 +0200321 return 0;
322
323 if (!s->txn) {
324 if (unlikely(!http_alloc_txn(s)))
325 return 0; /* not enough memory */
326 http_init_txn(s);
327 }
328 txn = s->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200329 smp->data.type = SMP_T_BOOL;
330
Christopher Faulet89dc4992019-04-17 12:02:59 +0200331 if (chn->flags & CF_ISRESP) {
332 /* Check for a dependency on a response */
333 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
334 smp->flags |= SMP_F_MAY_CHANGE;
335 return 0;
336 }
337 goto end;
338 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200339
Christopher Faulet89dc4992019-04-17 12:02:59 +0200340 /* Check for a dependency on a request */
341 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200342
Christopher Faulet89dc4992019-04-17 12:02:59 +0200343 if (req_vol && (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
344 return 0; /* data might have moved and indexes changed */
345 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200346
Christopher Faulet89dc4992019-04-17 12:02:59 +0200347 /* If the buffer does not leave enough free space at the end, we must
348 * first realign it.
349 */
350 if (ci_head(chn) > b_orig(&chn->buf) &&
351 ci_head(chn) + ci_data(chn) > b_wrap(&chn->buf) - global.tune.maxrewrite)
352 channel_slow_realign(chn, trash.area);
Willy Tarreau79e57332018-10-02 16:01:16 +0200353
Christopher Faulet89dc4992019-04-17 12:02:59 +0200354 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
355 if (msg->msg_state == HTTP_MSG_ERROR)
356 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200357
Christopher Faulet89dc4992019-04-17 12:02:59 +0200358 /* Try to decode HTTP request */
359 if (likely(msg->next < ci_data(chn)))
360 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau79e57332018-10-02 16:01:16 +0200361
Christopher Faulet89dc4992019-04-17 12:02:59 +0200362 /* Still no valid request ? */
363 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
364 if ((msg->msg_state == HTTP_MSG_ERROR) ||
365 channel_full(chn, global.tune.maxrewrite)) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200366 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200367 }
368 /* wait for final state */
369 smp->flags |= SMP_F_MAY_CHANGE;
370 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200371 }
372
Christopher Faulet89dc4992019-04-17 12:02:59 +0200373 /* OK we just got a valid HTTP message. We have some minor
374 * preparation to perform so that further checks can rely
375 * on HTTP tests.
376 */
377
378 /* If the message was parsed but was too large, we must absolutely
379 * return an error so that it is not processed. At the moment this
380 * cannot happen, but if the parsers are to change in the future,
381 * we want this check to be maintained.
382 */
383 if (unlikely(ci_head(chn) + ci_data(chn) >
384 b_wrap(&chn->buf) - global.tune.maxrewrite)) {
385 msg->err_state = msg->msg_state;
386 msg->msg_state = HTTP_MSG_ERROR;
387 smp->data.u.sint = 1;
388 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200389 }
390
Christopher Faulet89dc4992019-04-17 12:02:59 +0200391 txn->meth = find_http_meth(ci_head(chn), msg->sl.rq.m_l);
392 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
393 s->flags |= SF_REDIRECTABLE;
394
395 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau79e57332018-10-02 16:01:16 +0200396 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200397 }
398
Christopher Faulet89dc4992019-04-17 12:02:59 +0200399 end:
Willy Tarreau79e57332018-10-02 16:01:16 +0200400 /* everything's OK */
401 smp->data.u.sint = 1;
402 return 1;
403}
404
405/* This function fetches the method of current HTTP request and stores
406 * it in the global pattern struct as a chunk. There are two possibilities :
407 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
408 * in <len> and <ptr> is NULL ;
409 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
410 * <len> to its length.
411 * This is intended to be used with pat_match_meth() only.
412 */
413static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
414{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200415 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200416 int meth;
417 struct http_txn *txn;
418
Christopher Faulet46575cd2019-04-17 11:40:30 +0200419 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200420 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200421 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +0200422
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200423 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +0200424 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200425
426 txn = smp->strm->txn;
427 meth = txn->meth;
428 smp->data.type = SMP_T_METH;
429 smp->data.u.meth.meth = meth;
430 if (meth == HTTP_METH_OTHER) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100431 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200432
Christopher Faulet89dc4992019-04-17 12:02:59 +0200433 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200434 /* ensure the indexes are not affected */
435 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200436 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200437 sl = http_find_stline(htx);
438 smp->flags |= SMP_F_CONST;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100439 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
440 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200441 }
442 smp->flags |= SMP_F_VOL_1ST;
443 }
444 else {
445 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200446 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200447
448 txn = smp->strm->txn;
449 meth = txn->meth;
450 smp->data.type = SMP_T_METH;
451 smp->data.u.meth.meth = meth;
452 if (meth == HTTP_METH_OTHER) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200453 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200454 /* ensure the indexes are not affected */
455 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200456 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200457 smp->flags |= SMP_F_CONST;
458 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
459 smp->data.u.meth.str.area = ci_head(txn->req.chn);
460 }
461 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200462 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200463 return 1;
464}
465
466static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
467{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200468 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200469 struct http_txn *txn;
470 char *ptr;
471 int len;
472
Christopher Faulet46575cd2019-04-17 11:40:30 +0200473 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200474 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200475 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100476 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200477
478 if (!htx)
479 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200480
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200481 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100482 len = HTX_SL_REQ_VLEN(sl);
483 ptr = HTX_SL_REQ_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200484 }
485 else {
486 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200487 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200488
489 txn = smp->strm->txn;
490 len = txn->req.sl.rq.v_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200491 ptr = ci_head(chn) + txn->req.sl.rq.v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200492 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200493
494 while ((len-- > 0) && (*ptr++ != '/'));
495 if (len <= 0)
496 return 0;
497
498 smp->data.type = SMP_T_STR;
499 smp->data.u.str.area = ptr;
500 smp->data.u.str.data = len;
501
502 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
503 return 1;
504}
505
506static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
507{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200508 struct channel *chn = SMP_RES_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200509 struct http_txn *txn;
510 char *ptr;
511 int len;
512
Christopher Faulet46575cd2019-04-17 11:40:30 +0200513 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200514 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200515 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100516 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200517
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200518 if (!htx)
519 return 0;
520
521 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100522 len = HTX_SL_RES_VLEN(sl);
523 ptr = HTX_SL_RES_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200524 }
525 else {
526 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200527 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200528
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200529 txn = smp->strm->txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200530 len = txn->rsp.sl.st.v_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200531 ptr = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200532 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200533
534 while ((len-- > 0) && (*ptr++ != '/'));
535 if (len <= 0)
536 return 0;
537
538 smp->data.type = SMP_T_STR;
539 smp->data.u.str.area = ptr;
540 smp->data.u.str.data = len;
541
542 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
543 return 1;
544}
545
546/* 3. Check on Status Code. We manipulate integers here. */
547static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
548{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200549 struct channel *chn = SMP_RES_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200550 struct http_txn *txn;
551 char *ptr;
552 int len;
553
Christopher Faulet46575cd2019-04-17 11:40:30 +0200554 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200555 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200556 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100557 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200558
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200559 if (!htx)
560 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200561
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200562 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100563 len = HTX_SL_RES_CLEN(sl);
564 ptr = HTX_SL_RES_CPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200565 }
566 else {
567 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200568 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200569
570 txn = smp->strm->txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200571 len = txn->rsp.sl.st.c_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200572 ptr = ci_head(chn) + txn->rsp.sl.st.c;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200573 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200574
575 smp->data.type = SMP_T_SINT;
576 smp->data.u.sint = __strl2ui(ptr, len);
577 smp->flags = SMP_F_VOL_1ST;
578 return 1;
579}
580
581static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
582{
583 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
584 return 0;
585
586 if (!smp->strm->unique_id) {
587 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
588 return 0;
589 smp->strm->unique_id[0] = '\0';
590 }
591 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
592 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
593
594 smp->data.type = SMP_T_STR;
595 smp->data.u.str.area = smp->strm->unique_id;
596 smp->flags = SMP_F_CONST;
597 return 1;
598}
599
600/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800601 * empty line which separes headers from the body. This is useful
602 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200603 */
604static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
605{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200606 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200607 struct http_txn *txn;
608
Christopher Faulet46575cd2019-04-17 11:40:30 +0200609 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200610 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200611 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200612 struct buffer *temp;
613 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200614
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200615 if (!htx)
616 return 0;
617 temp = get_trash_chunk();
618 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
619 struct htx_blk *blk = htx_get_blk(htx, pos);
620 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200621
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200622 if (type == HTX_BLK_HDR) {
623 struct ist n = htx_get_blk_name(htx, blk);
624 struct ist v = htx_get_blk_value(htx, blk);
625
Christopher Fauletc59ff232018-12-03 13:58:44 +0100626 if (!htx_hdr_to_h1(n, v, temp))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200627 return 0;
628 }
629 else if (type == HTX_BLK_EOH) {
630 if (!chunk_memcat(temp, "\r\n", 2))
631 return 0;
632 break;
633 }
634 }
635 smp->data.type = SMP_T_STR;
636 smp->data.u.str = *temp;
637
638 }
639 else {
640 /* LEGACY version */
641 struct http_msg *msg;
642 struct hdr_idx *idx;
643
Christopher Faulet89dc4992019-04-17 12:02:59 +0200644 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200645
646 txn = smp->strm->txn;
647 idx = &txn->hdr_idx;
648 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200649
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200650 smp->data.type = SMP_T_STR;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200651 smp->data.u.str.area = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200652 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
Christopher Faulet89dc4992019-04-17 12:02:59 +0200653 (ci_head(chn)[msg->eoh] == '\r');
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200654 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200655 return 1;
656}
657
658/* Returns the header request in a length/value encoded format.
659 * This is useful for exchanges with the SPOE.
660 *
661 * A "length value" is a multibyte code encoding numbers. It uses the
662 * SPOE format. The encoding is the following:
663 *
664 * Each couple "header name" / "header value" is composed
665 * like this:
666 * "length value" "header name bytes"
667 * "length value" "header value bytes"
668 * When the last header is reached, the header name and the header
669 * value are empty. Their length are 0
670 */
671static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
672{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200673 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200674 struct http_txn *txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200675 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200676
Christopher Faulet46575cd2019-04-17 11:40:30 +0200677 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200678 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200679 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200680 struct buffer *temp;
681 char *p, *end;
682 int32_t pos;
683 int ret;
Willy Tarreau79e57332018-10-02 16:01:16 +0200684
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200685 if (!htx)
686 return 0;
687 temp = get_trash_chunk();
688 p = temp->area;
689 end = temp->area + temp->size;
690 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
691 struct htx_blk *blk = htx_get_blk(htx, pos);
692 enum htx_blk_type type = htx_get_blk_type(blk);
693 struct ist n, v;
Willy Tarreau79e57332018-10-02 16:01:16 +0200694
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200695 if (type == HTX_BLK_HDR) {
696 n = htx_get_blk_name(htx,blk);
697 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200698
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200699 /* encode the header name. */
700 ret = encode_varint(n.len, &p, end);
701 if (ret == -1)
702 return 0;
703 if (p + n.len > end)
704 return 0;
705 memcpy(p, n.ptr, n.len);
706 p += n.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200707
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200708 /* encode the header value. */
709 ret = encode_varint(v.len, &p, end);
710 if (ret == -1)
711 return 0;
712 if (p + v.len > end)
713 return 0;
714 memcpy(p, v.ptr, v.len);
715 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200716
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200717 }
718 else if (type == HTX_BLK_EOH) {
719 /* encode the end of the header list with empty
720 * header name and header value.
721 */
722 ret = encode_varint(0, &p, end);
723 if (ret == -1)
724 return 0;
725 ret = encode_varint(0, &p, end);
726 if (ret == -1)
727 return 0;
728 break;
729 }
730 }
731
732 /* Initialise sample data which will be filled. */
733 smp->data.type = SMP_T_BIN;
734 smp->data.u.str.area = temp->area;
735 smp->data.u.str.data = p - temp->area;
736 smp->data.u.str.size = temp->size;
737 }
738 else {
739 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200740 struct hdr_idx *idx;
741 const char *cur_ptr, *cur_next, *p;
742 int old_idx, cur_idx;
743 struct hdr_idx_elem *cur_hdr;
744 const char *hn, *hv;
745 int hnl, hvl;
746 int ret;
747 char *buf;
748 char *end;
749
Christopher Faulet89dc4992019-04-17 12:02:59 +0200750 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200751
752 temp = get_trash_chunk();
753 buf = temp->area;
754 end = temp->area + temp->size;
755
756 txn = smp->strm->txn;
757 idx = &txn->hdr_idx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200758
759 /* Build array of headers. */
760 old_idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200761 cur_next = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200762 while (1) {
763 cur_idx = idx->v[old_idx].next;
764 if (!cur_idx)
765 break;
766 old_idx = cur_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +0200767
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200768 cur_hdr = &idx->v[cur_idx];
769 cur_ptr = cur_next;
770 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
771
772 /* Now we have one full header at cur_ptr of len cur_hdr->len,
773 * and the next header starts at cur_next. We'll check
774 * this header in the list as well as against the default
775 * rule.
776 */
777
778 /* look for ': *'. */
779 hn = cur_ptr;
780 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
781 if (p >= cur_ptr+cur_hdr->len)
782 continue;
783 hnl = p - hn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200784 p++;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200785 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
786 p++;
787 if (p >= cur_ptr + cur_hdr->len)
788 continue;
789 hv = p;
790 hvl = cur_ptr + cur_hdr->len-p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200791
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200792 /* encode the header name. */
793 ret = encode_varint(hnl, &buf, end);
794 if (ret == -1)
795 return 0;
796 if (buf + hnl > end)
797 return 0;
798 memcpy(buf, hn, hnl);
799 buf += hnl;
800
801 /* encode and copy the value. */
802 ret = encode_varint(hvl, &buf, end);
803 if (ret == -1)
804 return 0;
805 if (buf + hvl > end)
806 return 0;
807 memcpy(buf, hv, hvl);
808 buf += hvl;
809 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200810
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200811 /* encode the end of the header list with empty
812 * header name and header value.
813 */
814 ret = encode_varint(0, &buf, end);
Willy Tarreau79e57332018-10-02 16:01:16 +0200815 if (ret == -1)
816 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200817 ret = encode_varint(0, &buf, end);
818 if (ret == -1)
Willy Tarreau79e57332018-10-02 16:01:16 +0200819 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200820
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200821 /* Initialise sample data which will be filled. */
822 smp->data.type = SMP_T_BIN;
823 smp->data.u.str.area = temp->area;
824 smp->data.u.str.data = buf - temp->area;
825 smp->data.u.str.size = temp->size;
826 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200827 return 1;
828}
829
830/* returns the longest available part of the body. This requires that the body
831 * has been waited for using http-buffer-request.
832 */
833static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
834{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200835 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200836 struct buffer *temp;
837
Christopher Faulet46575cd2019-04-17 11:40:30 +0200838 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200839 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200840 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200841 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200842
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200843 if (!htx)
844 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200845
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200846 temp = get_trash_chunk();
847 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
848 struct htx_blk *blk = htx_get_blk(htx, pos);
849 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200850
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200851 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
852 break;
853 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +0100854 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200855 return 0;
856 }
857 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200858
Willy Tarreau79e57332018-10-02 16:01:16 +0200859 smp->data.type = SMP_T_BIN;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200860 smp->data.u.str = *temp;
861 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200862 }
863 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200864 /* LEGACY version */
865 struct http_msg *msg;
866 unsigned long len;
867 unsigned long block1;
868 char *body;
869
Christopher Faulet89dc4992019-04-17 12:02:59 +0200870 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200871
Christopher Faulet89dc4992019-04-17 12:02:59 +0200872 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200873 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200874 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200875
876 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200877 if (block1 > b_wrap(&chn->buf) - body)
878 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200879
880 if (block1 == len) {
881 /* buffer is not wrapped (or empty) */
882 smp->data.type = SMP_T_BIN;
883 smp->data.u.str.area = body;
884 smp->data.u.str.data = len;
885 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
886 }
887 else {
888 /* buffer is wrapped, we need to defragment it */
889 temp = get_trash_chunk();
890 memcpy(temp->area, body, block1);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200891 memcpy(temp->area + block1, b_orig(&chn->buf), len - block1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200892 smp->data.type = SMP_T_BIN;
893 smp->data.u.str.area = temp->area;
894 smp->data.u.str.data = len;
895 smp->flags = SMP_F_VOL_TEST;
896 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200897 }
898 return 1;
899}
900
901
902/* returns the available length of the body. This requires that the body
903 * has been waited for using http-buffer-request.
904 */
905static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
906{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200907 struct channel *chn = SMP_REQ_CHN(smp);
908
Christopher Faulet46575cd2019-04-17 11:40:30 +0200909 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200910 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200911 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +0100912 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100913 unsigned long long len = 0;
914
915 if (!htx)
916 return 0;
917
Dragan Dosen5a606682019-02-14 12:30:53 +0100918 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
919 struct htx_blk *blk = htx_get_blk(htx, pos);
920 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100921
Dragan Dosen5a606682019-02-14 12:30:53 +0100922 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
Christopher Fauletc16317d2018-12-12 14:11:22 +0100923 break;
Dragan Dosen5a606682019-02-14 12:30:53 +0100924 if (type == HTX_BLK_DATA)
925 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100926 }
927
928 smp->data.type = SMP_T_SINT;
929 smp->data.u.sint = len;
930
931 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200932 }
933 else {
934 /* LEGACY version */
935 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200936
Christopher Faulet89dc4992019-04-17 12:02:59 +0200937 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200938
Christopher Faulet89dc4992019-04-17 12:02:59 +0200939 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200940 smp->data.type = SMP_T_SINT;
941 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreau79e57332018-10-02 16:01:16 +0200942
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200943 smp->flags = SMP_F_VOL_TEST;
944 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200945 return 1;
946}
947
948
949/* returns the advertised length of the body, or the advertised size of the
950 * chunks available in the buffer. This requires that the body has been waited
951 * for using http-buffer-request.
952 */
953static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
954{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200955 struct channel *chn = SMP_REQ_CHN(smp);
956
Christopher Faulet46575cd2019-04-17 11:40:30 +0200957 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200958 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200959 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +0100960 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100961 unsigned long long len = 0;
962
963 if (!htx)
964 return 0;
965
Dragan Dosen5a606682019-02-14 12:30:53 +0100966 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
967 struct htx_blk *blk = htx_get_blk(htx, pos);
968 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100969
Dragan Dosen5a606682019-02-14 12:30:53 +0100970 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
Christopher Fauletc16317d2018-12-12 14:11:22 +0100971 break;
Dragan Dosen5a606682019-02-14 12:30:53 +0100972 if (type == HTX_BLK_DATA)
973 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100974 }
975 if (htx->extra != ULLONG_MAX)
976 len += htx->extra;
977
978 smp->data.type = SMP_T_SINT;
979 smp->data.u.sint = len;
980
981 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200982 }
983 else {
984 /* LEGACY version */
985 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200986
Christopher Faulet89dc4992019-04-17 12:02:59 +0200987 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200988
Christopher Faulet89dc4992019-04-17 12:02:59 +0200989 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200990 smp->data.type = SMP_T_SINT;
991 smp->data.u.sint = msg->body_len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200992
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200993 smp->flags = SMP_F_VOL_TEST;
994 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200995 return 1;
996}
997
998
999/* 4. Check on URL/URI. A pointer to the URI is stored. */
1000static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
1001{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001002 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001003 struct http_txn *txn;
1004
Christopher Faulet46575cd2019-04-17 11:40:30 +02001005 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001006 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001007 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001008 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001009
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001010 if (!htx)
1011 return 0;
1012 sl = http_find_stline(htx);
1013 smp->data.type = SMP_T_STR;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001014 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
1015 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001016 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1017 }
1018 else {
1019 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001020 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001021
1022 txn = smp->strm->txn;
1023 smp->data.type = SMP_T_STR;
1024 smp->data.u.str.data = txn->req.sl.rq.u_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001025 smp->data.u.str.area = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001026 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1027 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001028 return 1;
1029}
1030
1031static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1032{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001033 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001034 struct http_txn *txn;
1035 struct sockaddr_storage addr;
1036
Christopher Faulet46575cd2019-04-17 11:40:30 +02001037 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001038 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001039 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001040 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001041
1042 if (!htx)
1043 return 0;
1044 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001045 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001046 }
1047 else {
1048 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001049 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001050
1051 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001052 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001053 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001054
Willy Tarreau79e57332018-10-02 16:01:16 +02001055 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1056 return 0;
1057
1058 smp->data.type = SMP_T_IPV4;
1059 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
1060 smp->flags = 0;
1061 return 1;
1062}
1063
1064static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
1065{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001066 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001067 struct http_txn *txn;
1068 struct sockaddr_storage addr;
1069
Christopher Faulet46575cd2019-04-17 11:40:30 +02001070 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001071 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001072 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001073 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001074
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001075 if (!htx)
1076 return 0;
1077 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001078 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001079 }
1080 else {
1081 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001082 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001083
1084 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001085 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001086 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001087 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1088 return 0;
1089
1090 smp->data.type = SMP_T_SINT;
1091 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
1092 smp->flags = 0;
1093 return 1;
1094}
1095
1096/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1097 * Accepts an optional argument of type string containing the header field name,
1098 * and an optional argument of type signed or unsigned integer to request an
1099 * explicit occurrence of the header. Note that in the event of a missing name,
1100 * headers are considered from the first one. It does not stop on commas and
1101 * returns full lines instead (useful for User-Agent or Date for example).
1102 */
1103static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1104{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001105 /* possible keywords: req.fhdr, res.fhdr */
1106 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001107 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001108
Christopher Faulet46575cd2019-04-17 11:40:30 +02001109 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001110 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001111 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001112 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1113 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +02001114
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001115 if (!ctx) {
1116 /* first call */
1117 ctx = &static_http_hdr_ctx;
1118 ctx->blk = NULL;
1119 smp->ctx.a[0] = ctx;
1120 }
1121
1122 if (args) {
1123 if (args[0].type != ARGT_STR)
1124 return 0;
1125 name.ptr = args[0].data.str.area;
1126 name.len = args[0].data.str.data;
1127
1128 if (args[1].type == ARGT_SINT)
1129 occ = args[1].data.sint;
1130 }
1131
1132 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001133 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001134
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001135 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1136 /* search for header from the beginning */
1137 ctx->blk = NULL;
1138
1139 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1140 /* no explicit occurrence and single fetch => last header by default */
1141 occ = -1;
1142
1143 if (!occ)
1144 /* prepare to report multiple occurrences for ACL fetches */
1145 smp->flags |= SMP_F_NOT_LAST;
1146
1147 smp->data.type = SMP_T_STR;
1148 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1149 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1150 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001151 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001152 else {
1153 /* LEGACY version */
1154 struct hdr_idx *idx;
1155 struct hdr_ctx *ctx = smp->ctx.a[0];
1156 const struct http_msg *msg;
1157 const char *name_str = NULL;
1158 int name_len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001159
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001160 if (!ctx) {
1161 /* first call */
1162 ctx = &static_hdr_ctx;
1163 ctx->idx = 0;
1164 smp->ctx.a[0] = ctx;
1165 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001166
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001167 if (args) {
1168 if (args[0].type != ARGT_STR)
1169 return 0;
1170 name_str = args[0].data.str.area;
1171 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001172
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001173 if (args[1].type == ARGT_SINT)
1174 occ = args[1].data.sint;
1175 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001176
Christopher Faulet89dc4992019-04-17 12:02:59 +02001177 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001178
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001179 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001180 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001181
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001182 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1183 /* search for header from the beginning */
1184 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001185
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001186 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1187 /* no explicit occurrence and single fetch => last header by default */
1188 occ = -1;
1189
1190 if (!occ)
1191 /* prepare to report multiple occurrences for ACL fetches */
1192 smp->flags |= SMP_F_NOT_LAST;
1193
1194 smp->data.type = SMP_T_STR;
1195 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1196 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1197 return 1;
1198 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001199 smp->flags &= ~SMP_F_NOT_LAST;
1200 return 0;
1201}
1202
1203/* 6. Check on HTTP header count. The number of occurrences is returned.
1204 * Accepts exactly 1 argument of type string. It does not stop on commas and
1205 * returns full lines instead (useful for User-Agent or Date for example).
1206 */
1207static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1208{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001209 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
1210 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001211 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001212
Christopher Faulet46575cd2019-04-17 11:40:30 +02001213 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001214 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001215 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001216 struct http_hdr_ctx ctx;
1217 struct ist name;
1218
1219 if (!htx)
1220 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001221
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001222 if (args && args->type == ARGT_STR) {
1223 name.ptr = args->data.str.area;
1224 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001225 } else {
1226 name.ptr = NULL;
1227 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001228 }
1229
1230 ctx.blk = NULL;
1231 cnt = 0;
1232 while (http_find_header(htx, name, &ctx, 1))
1233 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001234 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001235 else {
1236 /* LEGACY version */
1237 struct hdr_idx *idx;
1238 struct hdr_ctx ctx;
1239 const struct http_msg *msg;
1240 const char *name = NULL;
1241 int len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001242
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001243 if (args && args->type == ARGT_STR) {
1244 name = args->data.str.area;
1245 len = args->data.str.data;
1246 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001247
Christopher Faulet89dc4992019-04-17 12:02:59 +02001248 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001249
1250 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001251 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001252
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001253 ctx.idx = 0;
1254 cnt = 0;
1255 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
1256 cnt++;
1257 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001258
1259 smp->data.type = SMP_T_SINT;
1260 smp->data.u.sint = cnt;
1261 smp->flags = SMP_F_VOL_HDR;
1262 return 1;
1263}
1264
1265static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
1266{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001267 /* possible keywords: req.hdr_names, res.hdr_names */
1268 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001269 struct buffer *temp;
1270 char del = ',';
1271
Christopher Faulet46575cd2019-04-17 11:40:30 +02001272 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001273 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001274 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001275 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001276
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001277 if (!htx)
1278 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001279
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001280 if (args && args->type == ARGT_STR)
1281 del = *args[0].data.str.area;
Willy Tarreau79e57332018-10-02 16:01:16 +02001282
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001283 temp = get_trash_chunk();
1284 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1285 struct htx_blk *blk = htx_get_blk(htx, pos);
1286 enum htx_blk_type type = htx_get_blk_type(blk);
1287 struct ist n;
Willy Tarreau79e57332018-10-02 16:01:16 +02001288
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001289 if (type == HTX_BLK_EOH)
1290 break;
1291 if (type != HTX_BLK_HDR)
1292 continue;
1293 n = htx_get_blk_name(htx, blk);
1294
1295 if (temp->data)
1296 temp->area[temp->data++] = del;
1297 chunk_memcat(temp, n.ptr, n.len);
1298 }
1299 }
1300 else {
1301 /* LEGACY version */
1302 struct hdr_idx *idx;
1303 struct hdr_ctx ctx;
1304 const struct http_msg *msg;
1305
1306 if (args && args->type == ARGT_STR)
1307 del = *args[0].data.str.area;
1308
Christopher Faulet89dc4992019-04-17 12:02:59 +02001309 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001310
1311 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001312 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001313
1314 temp = get_trash_chunk();
1315
1316 ctx.idx = 0;
1317 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
1318 if (temp->data)
1319 temp->area[temp->data++] = del;
1320 memcpy(temp->area + temp->data, ctx.line, ctx.del);
1321 temp->data += ctx.del;
1322 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001323 }
1324
1325 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001326 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001327 smp->flags = SMP_F_VOL_HDR;
1328 return 1;
1329}
1330
1331/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1332 * Accepts an optional argument of type string containing the header field name,
1333 * and an optional argument of type signed or unsigned integer to request an
1334 * explicit occurrence of the header. Note that in the event of a missing name,
1335 * headers are considered from the first one.
1336 */
1337static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1338{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001339 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
1340 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001341 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001342
Christopher Faulet46575cd2019-04-17 11:40:30 +02001343 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001344 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001345 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001346 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1347 struct ist name;
1348
1349 if (!ctx) {
1350 /* first call */
1351 ctx = &static_http_hdr_ctx;
1352 ctx->blk = NULL;
1353 smp->ctx.a[0] = ctx;
1354 }
1355
1356 if (args) {
1357 if (args[0].type != ARGT_STR)
1358 return 0;
1359 name.ptr = args[0].data.str.area;
1360 name.len = args[0].data.str.data;
1361
1362 if (args[1].type == ARGT_SINT)
1363 occ = args[1].data.sint;
1364 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001365
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001366 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001367 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001368
1369 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1370 /* search for header from the beginning */
1371 ctx->blk = NULL;
1372
1373 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1374 /* no explicit occurrence and single fetch => last header by default */
1375 occ = -1;
1376
1377 if (!occ)
1378 /* prepare to report multiple occurrences for ACL fetches */
1379 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001380
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001381 smp->data.type = SMP_T_STR;
1382 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1383 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1384 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001385 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001386 else {
1387 /* LEGACY version */
1388 struct hdr_idx *idx;
1389 struct hdr_ctx *ctx = smp->ctx.a[0];
1390 const struct http_msg *msg;
1391 const char *name_str = NULL;
1392 int name_len = 0;
1393
1394 if (!ctx) {
1395 /* first call */
1396 ctx = &static_hdr_ctx;
1397 ctx->idx = 0;
1398 smp->ctx.a[0] = ctx;
1399 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001400
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001401 if (args) {
1402 if (args[0].type != ARGT_STR)
1403 return 0;
1404 name_str = args[0].data.str.area;
1405 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001406
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001407 if (args[1].type == ARGT_SINT)
1408 occ = args[1].data.sint;
1409 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001410
Christopher Faulet89dc4992019-04-17 12:02:59 +02001411 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001412
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001413 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001414 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001415
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001416 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1417 /* search for header from the beginning */
1418 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001419
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001420 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1421 /* no explicit occurrence and single fetch => last header by default */
1422 occ = -1;
1423
1424 if (!occ)
1425 /* prepare to report multiple occurrences for ACL fetches */
1426 smp->flags |= SMP_F_NOT_LAST;
1427
1428 smp->data.type = SMP_T_STR;
1429 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1430 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1431 return 1;
1432 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001433
1434 smp->flags &= ~SMP_F_NOT_LAST;
1435 return 0;
1436}
1437
1438/* 6. Check on HTTP header count. The number of occurrences is returned.
1439 * Accepts exactly 1 argument of type string.
1440 */
1441static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1442{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001443 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
1444 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001445 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001446
Christopher Faulet46575cd2019-04-17 11:40:30 +02001447 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001448 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001449 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001450 struct http_hdr_ctx ctx;
1451 struct ist name;
1452
1453 if (!htx)
1454 return 0;
1455
1456 if (args && args->type == ARGT_STR) {
1457 name.ptr = args->data.str.area;
1458 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001459 } else {
1460 name.ptr = NULL;
1461 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001462 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001463
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001464 ctx.blk = NULL;
1465 cnt = 0;
1466 while (http_find_header(htx, name, &ctx, 0))
1467 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001468 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001469 else {
1470 /* LEGACY version */
1471 struct hdr_idx *idx;
1472 struct hdr_ctx ctx;
1473 const struct http_msg *msg;
1474 const char *name = NULL;
1475 int len = 0;
1476
1477 if (args && args->type == ARGT_STR) {
1478 name = args->data.str.area;
1479 len = args->data.str.data;
1480 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001481
Christopher Faulet89dc4992019-04-17 12:02:59 +02001482 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001483
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001484 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001485 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001486
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001487 ctx.idx = 0;
1488 cnt = 0;
1489 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
1490 cnt++;
1491 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001492
1493 smp->data.type = SMP_T_SINT;
1494 smp->data.u.sint = cnt;
1495 smp->flags = SMP_F_VOL_HDR;
1496 return 1;
1497}
1498
1499/* Fetch an HTTP header's integer value. The integer value is returned. It
1500 * takes a mandatory argument of type string and an optional one of type int
1501 * to designate a specific occurrence. It returns an unsigned integer, which
1502 * may or may not be appropriate for everything.
1503 */
1504static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1505{
1506 int ret = smp_fetch_hdr(args, smp, kw, private);
1507
1508 if (ret > 0) {
1509 smp->data.type = SMP_T_SINT;
1510 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1511 smp->data.u.str.data);
1512 }
1513
1514 return ret;
1515}
1516
1517/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1518 * and an optional one of type int to designate a specific occurrence.
1519 * It returns an IPv4 or IPv6 address.
1520 */
1521static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1522{
1523 int ret;
1524
1525 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
1526 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
1527 smp->data.type = SMP_T_IPV4;
1528 break;
1529 } else {
1530 struct buffer *temp = get_trash_chunk();
1531 if (smp->data.u.str.data < temp->size - 1) {
1532 memcpy(temp->area, smp->data.u.str.area,
1533 smp->data.u.str.data);
1534 temp->area[smp->data.u.str.data] = '\0';
1535 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1536 smp->data.type = SMP_T_IPV6;
1537 break;
1538 }
1539 }
1540 }
1541
1542 /* if the header doesn't match an IP address, fetch next one */
1543 if (!(smp->flags & SMP_F_NOT_LAST))
1544 return 0;
1545 }
1546 return ret;
1547}
1548
1549/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1550 * the first '/' after the possible hostname, and ends before the possible '?'.
1551 */
1552static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1553{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001554 struct channel *chn = SMP_REQ_CHN(smp);
1555
Christopher Faulet46575cd2019-04-17 11:40:30 +02001556 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001557 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001558 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001559 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001560 struct ist path;
1561 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001562
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001563 if (!htx)
1564 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001565
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001566 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001567 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001568 if (!path.ptr)
1569 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001570
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001571 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001572 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001573
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001574 /* OK, we got the '/' ! */
1575 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001576 smp->data.u.str.area = path.ptr;
1577 smp->data.u.str.data = len;
1578 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1579 }
1580 else {
1581 struct http_txn *txn;
1582 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001583
Christopher Faulet89dc4992019-04-17 12:02:59 +02001584 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001585
1586 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001587 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001588 ptr = http_txn_get_path(txn);
1589 if (!ptr)
1590 return 0;
1591
1592 /* OK, we got the '/' ! */
1593 smp->data.type = SMP_T_STR;
1594 smp->data.u.str.area = ptr;
1595
1596 while (ptr < end && *ptr != '?')
1597 ptr++;
1598
1599 smp->data.u.str.data = ptr - smp->data.u.str.area;
1600 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1601 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001602 return 1;
1603}
1604
1605/* This produces a concatenation of the first occurrence of the Host header
1606 * followed by the path component if it begins with a slash ('/'). This means
1607 * that '*' will not be added, resulting in exactly the first Host entry.
1608 * If no Host header is found, then the path is returned as-is. The returned
1609 * value is stored in the trash so it does not need to be marked constant.
1610 * The returned sample is of type string.
1611 */
1612static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1613{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001614 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001615 struct buffer *temp;
1616
Christopher Faulet46575cd2019-04-17 11:40:30 +02001617 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001618 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001619 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001620 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001621 struct http_hdr_ctx ctx;
1622 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001623
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001624 if (!htx)
1625 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001626
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001627 ctx.blk = NULL;
1628 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1629 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001630
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001631 /* OK we have the header value in ctx.value */
1632 temp = get_trash_chunk();
1633 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1634
1635 /* now retrieve the path */
1636 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001637 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001638 if (path.ptr) {
1639 size_t len;
1640
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001641 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1642 ;
1643
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001644 if (len && *(path.ptr) == '/')
1645 chunk_memcat(temp, path.ptr, len);
1646 }
1647
1648 smp->data.type = SMP_T_STR;
1649 smp->data.u.str = *temp;
1650 }
1651 else {
1652 /* LEGACY version */
1653 struct http_txn *txn;
1654 char *ptr, *end, *beg;
1655 struct hdr_ctx ctx;
1656
Christopher Faulet89dc4992019-04-17 12:02:59 +02001657 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001658
1659 txn = smp->strm->txn;
1660 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001661 if (!http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001662 return smp_fetch_path(args, smp, kw, private);
1663
1664 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1665 temp = get_trash_chunk();
1666 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1667 smp->data.type = SMP_T_STR;
1668 smp->data.u.str.area = temp->area;
1669 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001670
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001671 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001672 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001673 beg = http_txn_get_path(txn);
1674 if (!beg)
1675 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001676
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001677 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1678
1679 if (beg < ptr && *beg == '/') {
1680 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1681 ptr - beg);
1682 smp->data.u.str.data += ptr - beg;
1683 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001684 }
1685
1686 smp->flags = SMP_F_VOL_1ST;
1687 return 1;
1688}
1689
1690/* This produces a 32-bit hash of the concatenation of the first occurrence of
1691 * the Host header followed by the path component if it begins with a slash ('/').
1692 * This means that '*' will not be added, resulting in exactly the first Host
1693 * entry. If no Host header is found, then the path is used. The resulting value
1694 * is hashed using the path hash followed by a full avalanche hash and provides a
1695 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1696 * high-traffic sites without having to store whole paths.
1697 */
1698static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1699{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001700 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001701 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001702
Christopher Faulet46575cd2019-04-17 11:40:30 +02001703 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001704 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001705 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001706 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001707 struct http_hdr_ctx ctx;
1708 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001709
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001710 if (!htx)
1711 return 0;
1712
1713 ctx.blk = NULL;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001714 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001715 /* OK we have the header value in ctx.value */
1716 while (ctx.value.len--)
1717 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1718 }
1719
1720 /* now retrieve the path */
1721 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001722 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001723 if (path.ptr) {
1724 size_t len;
1725
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001726 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1727 ;
1728
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001729 if (len && *(path.ptr) == '/') {
1730 while (len--)
1731 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1732 }
1733 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001734 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001735 else {
1736 /* LEGACY version */
1737 struct http_txn *txn;
1738 struct hdr_ctx ctx;
1739 char *ptr, *beg, *end;
1740 int len;
1741
Christopher Faulet89dc4992019-04-17 12:02:59 +02001742 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001743
1744 txn = smp->strm->txn;
1745 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001746 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001747 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1748 ptr = ctx.line + ctx.val;
1749 len = ctx.vlen;
1750 while (len--)
1751 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1752 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001753
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001754 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001755 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001756 beg = http_txn_get_path(txn);
1757 if (!beg)
1758 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001759
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001760 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001761
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001762 if (beg < ptr && *beg == '/') {
1763 while (beg < ptr)
1764 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
1765 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001766 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001767
Willy Tarreau79e57332018-10-02 16:01:16 +02001768 hash = full_hash(hash);
1769
1770 smp->data.type = SMP_T_SINT;
1771 smp->data.u.sint = hash;
1772 smp->flags = SMP_F_VOL_1ST;
1773 return 1;
1774}
1775
1776/* This concatenates the source address with the 32-bit hash of the Host and
1777 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1778 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1779 * on the source address length. The path hash is stored before the address so
1780 * that in environments where IPv6 is insignificant, truncating the output to
1781 * 8 bytes would still work.
1782 */
1783static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1784{
1785 struct buffer *temp;
1786 struct connection *cli_conn = objt_conn(smp->sess->origin);
1787
1788 if (!cli_conn)
1789 return 0;
1790
1791 if (!smp_fetch_base32(args, smp, kw, private))
1792 return 0;
1793
1794 temp = get_trash_chunk();
1795 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1796 temp->data += sizeof(unsigned int);
1797
1798 switch (cli_conn->addr.from.ss_family) {
1799 case AF_INET:
1800 memcpy(temp->area + temp->data,
1801 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1802 4);
1803 temp->data += 4;
1804 break;
1805 case AF_INET6:
1806 memcpy(temp->area + temp->data,
1807 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1808 16);
1809 temp->data += 16;
1810 break;
1811 default:
1812 return 0;
1813 }
1814
1815 smp->data.u.str = *temp;
1816 smp->data.type = SMP_T_BIN;
1817 return 1;
1818}
1819
1820/* Extracts the query string, which comes after the question mark '?'. If no
1821 * question mark is found, nothing is returned. Otherwise it returns a sample
1822 * of type string carrying the whole query string.
1823 */
1824static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1825{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001826 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001827 char *ptr, *end;
1828
Christopher Faulet46575cd2019-04-17 11:40:30 +02001829 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001830 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001831 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001832 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001833
1834 if (!htx)
1835 return 0;
1836
1837 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001838 ptr = HTX_SL_REQ_UPTR(sl);
1839 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001840 }
1841 else {
1842 /* LEGACY version */
1843 struct http_txn *txn;
1844
Christopher Faulet89dc4992019-04-17 12:02:59 +02001845 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001846
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001847 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001848 ptr = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001849 end = ptr + txn->req.sl.rq.u_l;
1850 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001851
1852 /* look up the '?' */
1853 do {
1854 if (ptr == end)
1855 return 0;
1856 } while (*ptr++ != '?');
1857
1858 smp->data.type = SMP_T_STR;
1859 smp->data.u.str.area = ptr;
1860 smp->data.u.str.data = end - ptr;
1861 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1862 return 1;
1863}
1864
1865static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1866{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001867 struct channel *chn = SMP_REQ_CHN(smp);
1868
Christopher Faulet46575cd2019-04-17 11:40:30 +02001869 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001870 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001871 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001872
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001873 if (!htx)
1874 return 0;
1875 }
1876 else {
1877 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02001878
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001879 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
1880 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
1881 */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001882 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001883 }
1884 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001885 smp->data.u.sint = 1;
1886 return 1;
1887}
1888
1889/* return a valid test if the current request is the first one on the connection */
1890static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1891{
1892 smp->data.type = SMP_T_BOOL;
1893 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1894 return 1;
1895}
1896
1897/* Accepts exactly 1 argument of type userlist */
1898static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1899{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001900 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001901
1902 if (!args || args->type != ARGT_USR)
1903 return 0;
1904
Christopher Faulet46575cd2019-04-17 11:40:30 +02001905 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001906 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001907 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001908
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001909 if (!htx)
1910 return 0;
1911 }
1912 else {
1913 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001914 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001915 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001916
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001917 if (!get_http_auth(smp))
1918 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001919 smp->data.type = SMP_T_BOOL;
1920 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001921 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001922 return 1;
1923}
1924
1925/* Accepts exactly 1 argument of type userlist */
1926static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1927{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001928 struct channel *chn = SMP_REQ_CHN(smp);
1929
Willy Tarreau79e57332018-10-02 16:01:16 +02001930 if (!args || args->type != ARGT_USR)
1931 return 0;
1932
Christopher Faulet46575cd2019-04-17 11:40:30 +02001933 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001934 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001935 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001936
1937 if (!htx)
1938 return 0;
1939 }
1940 else {
1941 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001942 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001943 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001944
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001945 if (!get_http_auth(smp))
Willy Tarreau79e57332018-10-02 16:01:16 +02001946 return 0;
1947
1948 /* if the user does not belong to the userlist or has a wrong password,
1949 * report that it unconditionally does not match. Otherwise we return
1950 * a string containing the username.
1951 */
1952 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1953 smp->strm->txn->auth.pass))
1954 return 0;
1955
1956 /* pat_match_auth() will need the user list */
1957 smp->ctx.a[0] = args->data.usr;
1958
1959 smp->data.type = SMP_T_STR;
1960 smp->flags = SMP_F_CONST;
1961 smp->data.u.str.area = smp->strm->txn->auth.user;
1962 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1963
1964 return 1;
1965}
1966
1967/* Fetch a captured HTTP request header. The index is the position of
1968 * the "capture" option in the configuration file
1969 */
1970static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1971{
1972 struct proxy *fe = strm_fe(smp->strm);
1973 int idx;
1974
1975 if (!args || args->type != ARGT_SINT)
1976 return 0;
1977
1978 idx = args->data.sint;
1979
1980 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1981 return 0;
1982
1983 smp->data.type = SMP_T_STR;
1984 smp->flags |= SMP_F_CONST;
1985 smp->data.u.str.area = smp->strm->req_cap[idx];
1986 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1987
1988 return 1;
1989}
1990
1991/* Fetch a captured HTTP response header. The index is the position of
1992 * the "capture" option in the configuration file
1993 */
1994static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1995{
1996 struct proxy *fe = strm_fe(smp->strm);
1997 int idx;
1998
1999 if (!args || args->type != ARGT_SINT)
2000 return 0;
2001
2002 idx = args->data.sint;
2003
2004 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
2005 return 0;
2006
2007 smp->data.type = SMP_T_STR;
2008 smp->flags |= SMP_F_CONST;
2009 smp->data.u.str.area = smp->strm->res_cap[idx];
2010 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
2011
2012 return 1;
2013}
2014
2015/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
2016static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
2017{
2018 struct buffer *temp;
2019 struct http_txn *txn = smp->strm->txn;
2020 char *ptr;
2021
2022 if (!txn || !txn->uri)
2023 return 0;
2024
2025 ptr = txn->uri;
2026
2027 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2028 ptr++;
2029
2030 temp = get_trash_chunk();
2031 temp->area = txn->uri;
2032 temp->data = ptr - txn->uri;
2033 smp->data.u.str = *temp;
2034 smp->data.type = SMP_T_STR;
2035 smp->flags = SMP_F_CONST;
2036
2037 return 1;
2038
2039}
2040
2041/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
2042static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
2043{
2044 struct http_txn *txn = smp->strm->txn;
2045 struct ist path;
2046 const char *ptr;
2047
2048 if (!txn || !txn->uri)
2049 return 0;
2050
2051 ptr = txn->uri;
2052
2053 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2054 ptr++;
2055
2056 if (!*ptr)
2057 return 0;
2058
Christopher Faulet78337bb2018-11-15 14:35:18 +01002059 /* skip the first space and find space after URI */
2060 path = ist2(++ptr, 0);
2061 while (*ptr != ' ' && *ptr != '\0')
2062 ptr++;
2063 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002064
Christopher Faulet78337bb2018-11-15 14:35:18 +01002065 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02002066 if (!path.ptr)
2067 return 0;
2068
2069 smp->data.u.str.area = path.ptr;
2070 smp->data.u.str.data = path.len;
2071 smp->data.type = SMP_T_STR;
2072 smp->flags = SMP_F_CONST;
2073
2074 return 1;
2075}
2076
2077/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
2078 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2079 */
2080static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2081{
2082 struct http_txn *txn = smp->strm->txn;
2083
2084 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2085 return 0;
2086
2087 if (txn->req.flags & HTTP_MSGF_VER_11)
2088 smp->data.u.str.area = "HTTP/1.1";
2089 else
2090 smp->data.u.str.area = "HTTP/1.0";
2091
2092 smp->data.u.str.data = 8;
2093 smp->data.type = SMP_T_STR;
2094 smp->flags = SMP_F_CONST;
2095 return 1;
2096
2097}
2098
2099/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2100 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2101 */
2102static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2103{
2104 struct http_txn *txn = smp->strm->txn;
2105
2106 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2107 return 0;
2108
2109 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2110 smp->data.u.str.area = "HTTP/1.1";
2111 else
2112 smp->data.u.str.area = "HTTP/1.0";
2113
2114 smp->data.u.str.data = 8;
2115 smp->data.type = SMP_T_STR;
2116 smp->flags = SMP_F_CONST;
2117 return 1;
2118
2119}
2120
2121/* Iterate over all cookies present in a message. The context is stored in
2122 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2123 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2124 * the direction, multiple cookies may be parsed on the same line or not.
2125 * The cookie name is in args and the name length in args->data.str.len.
2126 * Accepts exactly 1 argument of type string. If the input options indicate
2127 * that no iterating is desired, then only last value is fetched if any.
2128 * The returned sample is of type CSTR. Can be used to parse cookies in other
2129 * files.
2130 */
2131static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2132{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002133 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
2134 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002135 int occ = 0;
2136 int found = 0;
2137
2138 if (!args || args->type != ARGT_STR)
2139 return 0;
2140
Christopher Faulet46575cd2019-04-17 11:40:30 +02002141 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002142 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002143 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002144 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2145 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002146
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002147 if (!ctx) {
2148 /* first call */
2149 ctx = &static_http_hdr_ctx;
2150 ctx->blk = NULL;
2151 smp->ctx.a[2] = ctx;
2152 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002153
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002154 if (!htx)
2155 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002156
Christopher Faulet89dc4992019-04-17 12:02:59 +02002157 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002158
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002159 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2160 /* no explicit occurrence and single fetch => last cookie by default */
2161 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02002162
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002163 /* OK so basically here, either we want only one value and it's the
2164 * last one, or we want to iterate over all of them and we fetch the
2165 * next one.
Willy Tarreau79e57332018-10-02 16:01:16 +02002166 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002167
2168 if (!(smp->flags & SMP_F_NOT_LAST)) {
2169 /* search for the header from the beginning, we must first initialize
2170 * the search parameters.
2171 */
2172 smp->ctx.a[0] = NULL;
2173 ctx->blk = NULL;
2174 }
2175
2176 smp->flags |= SMP_F_VOL_HDR;
2177 while (1) {
2178 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2179 if (!smp->ctx.a[0]) {
2180 if (!http_find_header(htx, hdr, ctx, 0))
2181 goto out;
2182
2183 if (ctx->value.len < args->data.str.data + 1)
2184 continue;
2185
2186 smp->ctx.a[0] = ctx->value.ptr;
2187 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2188 }
2189
2190 smp->data.type = SMP_T_STR;
2191 smp->flags |= SMP_F_CONST;
2192 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2193 args->data.str.area, args->data.str.data,
2194 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2195 &smp->data.u.str.area,
2196 &smp->data.u.str.data);
2197 if (smp->ctx.a[0]) {
2198 found = 1;
2199 if (occ >= 0) {
2200 /* one value was returned into smp->data.u.str.{str,len} */
2201 smp->flags |= SMP_F_NOT_LAST;
2202 return 1;
2203 }
2204 }
2205 /* if we're looking for last occurrence, let's loop */
2206 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002207 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002208 else {
2209 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002210 struct hdr_idx *idx;
2211 struct hdr_ctx *ctx = smp->ctx.a[2];
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002212 const char *hdr_name;
2213 int hdr_name_len;
2214 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002215
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002216 if (!ctx) {
2217 /* first call */
2218 ctx = &static_hdr_ctx;
2219 ctx->idx = 0;
2220 smp->ctx.a[2] = ctx;
2221 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002222
Christopher Faulet89dc4992019-04-17 12:02:59 +02002223 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002224
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002225 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002226 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002227 hdr_name = "Cookie";
2228 hdr_name_len = 6;
2229 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002230 hdr_name = "Set-Cookie";
2231 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002232 }
2233
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002234 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2235 /* no explicit occurrence and single fetch => last cookie by default */
2236 occ = -1;
2237
2238 /* OK so basically here, either we want only one value and it's the
2239 * last one, or we want to iterate over all of them and we fetch the
2240 * next one.
2241 */
2242
Christopher Faulet89dc4992019-04-17 12:02:59 +02002243 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002244 if (!(smp->flags & SMP_F_NOT_LAST)) {
2245 /* search for the header from the beginning, we must first initialize
2246 * the search parameters.
2247 */
2248 smp->ctx.a[0] = NULL;
2249 ctx->idx = 0;
2250 }
2251
2252 smp->flags |= SMP_F_VOL_HDR;
2253
2254 while (1) {
2255 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2256 if (!smp->ctx.a[0]) {
2257 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2258 goto out;
2259
2260 if (ctx->vlen < args->data.str.data + 1)
2261 continue;
2262
2263 smp->ctx.a[0] = ctx->line + ctx->val;
2264 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2265 }
2266
2267 smp->data.type = SMP_T_STR;
2268 smp->flags |= SMP_F_CONST;
2269 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2270 args->data.str.area, args->data.str.data,
2271 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2272 &smp->data.u.str.area, &smp->data.u.str.data);
2273 if (smp->ctx.a[0]) {
2274 found = 1;
2275 if (occ >= 0) {
2276 /* one value was returned into smp->data.u.str.{str,len} */
2277 smp->flags |= SMP_F_NOT_LAST;
2278 return 1;
2279 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002280 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002281 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002282 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002283 }
2284 /* all cookie headers and values were scanned. If we're looking for the
2285 * last occurrence, we may return it now.
2286 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002287 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002288 smp->flags &= ~SMP_F_NOT_LAST;
2289 return found;
2290}
2291
2292/* Iterate over all cookies present in a request to count how many occurrences
2293 * match the name in args and args->data.str.len. If <multi> is non-null, then
2294 * multiple cookies may be parsed on the same line. The returned sample is of
2295 * type UINT. Accepts exactly 1 argument of type string.
2296 */
2297static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2298{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002299 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
2300 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002301 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002302 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002303
2304 if (!args || args->type != ARGT_STR)
2305 return 0;
2306
Christopher Faulet46575cd2019-04-17 11:40:30 +02002307 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002308 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002309 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002310 struct http_hdr_ctx ctx;
2311 struct ist hdr;
2312
2313 if (!htx)
2314 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002315
Christopher Faulet89dc4992019-04-17 12:02:59 +02002316 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002317
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002318 val_end = val_beg = NULL;
2319 ctx.blk = NULL;
2320 cnt = 0;
2321 while (1) {
2322 /* Note: val_beg == NULL every time we need to fetch a new header */
2323 if (!val_beg) {
2324 if (!http_find_header(htx, hdr, &ctx, 0))
2325 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002326
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002327 if (ctx.value.len < args->data.str.data + 1)
2328 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002329
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002330 val_beg = ctx.value.ptr;
2331 val_end = val_beg + ctx.value.len;
2332 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002333
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002334 smp->data.type = SMP_T_STR;
2335 smp->flags |= SMP_F_CONST;
2336 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2337 args->data.str.area, args->data.str.data,
2338 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2339 &smp->data.u.str.area,
2340 &smp->data.u.str.data))) {
2341 cnt++;
2342 }
2343 }
2344 }
2345 else {
2346 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002347 struct hdr_idx *idx;
2348 struct hdr_ctx ctx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002349 const char *hdr_name;
2350 int hdr_name_len;
2351 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002352
Christopher Faulet89dc4992019-04-17 12:02:59 +02002353 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002354
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002355 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002356 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002357 hdr_name = "Cookie";
2358 hdr_name_len = 6;
2359 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002360 hdr_name = "Set-Cookie";
2361 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002362 }
2363
Christopher Faulet89dc4992019-04-17 12:02:59 +02002364 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002365 val_end = val_beg = NULL;
2366 ctx.idx = 0;
2367 cnt = 0;
2368
2369 while (1) {
2370 /* Note: val_beg == NULL every time we need to fetch a new header */
2371 if (!val_beg) {
2372 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2373 break;
2374
2375 if (ctx.vlen < args->data.str.data + 1)
2376 continue;
2377
2378 val_beg = ctx.line + ctx.val;
2379 val_end = val_beg + ctx.vlen;
2380 }
2381
2382 smp->data.type = SMP_T_STR;
2383 smp->flags |= SMP_F_CONST;
2384 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2385 args->data.str.area, args->data.str.data,
2386 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2387 &smp->data.u.str.area, &smp->data.u.str.data))) {
2388 cnt++;
2389 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002390 }
2391 }
2392
2393 smp->data.type = SMP_T_SINT;
2394 smp->data.u.sint = cnt;
2395 smp->flags |= SMP_F_VOL_HDR;
2396 return 1;
2397}
2398
2399/* Fetch an cookie's integer value. The integer value is returned. It
2400 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2401 */
2402static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2403{
2404 int ret = smp_fetch_cookie(args, smp, kw, private);
2405
2406 if (ret > 0) {
2407 smp->data.type = SMP_T_SINT;
2408 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2409 smp->data.u.str.data);
2410 }
2411
2412 return ret;
2413}
2414
2415/************************************************************************/
2416/* The code below is dedicated to sample fetches */
2417/************************************************************************/
2418
2419/* This scans a URL-encoded query string. It takes an optionally wrapping
2420 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2421 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2422 * pointers are updated for next iteration before leaving.
2423 */
2424static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2425{
2426 const char *vstart, *vend;
2427 struct buffer *temp;
2428 const char **chunks = (const char **)smp->ctx.a;
2429
2430 if (!http_find_next_url_param(chunks, name, name_len,
2431 &vstart, &vend, delim))
2432 return 0;
2433
2434 /* Create sample. If the value is contiguous, return the pointer as CONST,
2435 * if the value is wrapped, copy-it in a buffer.
2436 */
2437 smp->data.type = SMP_T_STR;
2438 if (chunks[2] &&
2439 vstart >= chunks[0] && vstart <= chunks[1] &&
2440 vend >= chunks[2] && vend <= chunks[3]) {
2441 /* Wrapped case. */
2442 temp = get_trash_chunk();
2443 memcpy(temp->area, vstart, chunks[1] - vstart);
2444 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2445 vend - chunks[2]);
2446 smp->data.u.str.area = temp->area;
2447 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2448 } else {
2449 /* Contiguous case. */
2450 smp->data.u.str.area = (char *)vstart;
2451 smp->data.u.str.data = vend - vstart;
2452 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2453 }
2454
2455 /* Update context, check wrapping. */
2456 chunks[0] = vend;
2457 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2458 chunks[1] = chunks[3];
2459 chunks[2] = NULL;
2460 }
2461
2462 if (chunks[0] < chunks[1])
2463 smp->flags |= SMP_F_NOT_LAST;
2464
2465 return 1;
2466}
2467
2468/* This function iterates over each parameter of the query string. It uses
2469 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2470 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2471 * An optional parameter name is passed in args[0], otherwise any parameter is
2472 * considered. It supports an optional delimiter argument for the beginning of
2473 * the string in args[1], which defaults to "?".
2474 */
2475static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2476{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002477 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002478 char delim = '?';
2479 const char *name;
2480 int name_len;
2481
2482 if (!args ||
2483 (args[0].type && args[0].type != ARGT_STR) ||
2484 (args[1].type && args[1].type != ARGT_STR))
2485 return 0;
2486
2487 name = "";
2488 name_len = 0;
2489 if (args->type == ARGT_STR) {
2490 name = args->data.str.area;
2491 name_len = args->data.str.data;
2492 }
2493
2494 if (args[1].type)
2495 delim = *args[1].data.str.area;
2496
2497 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002498 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002499 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002500 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002501 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002502
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002503 if (!htx)
2504 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002505
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002506 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002507 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002508 if (!smp->ctx.a[0])
2509 return 0;
2510
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002511 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002512 }
2513 else {
2514 /* LEGACY version */
2515 struct http_msg *msg;
2516
Christopher Faulet89dc4992019-04-17 12:02:59 +02002517 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002518
2519 msg = &smp->strm->txn->req;
2520
Christopher Faulet89dc4992019-04-17 12:02:59 +02002521 smp->ctx.a[0] = http_find_param_list(ci_head(chn) + msg->sl.rq.u,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002522 msg->sl.rq.u_l, delim);
2523 if (!smp->ctx.a[0])
2524 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002525
Christopher Faulet89dc4992019-04-17 12:02:59 +02002526 smp->ctx.a[1] = ci_head(chn) + msg->sl.rq.u + msg->sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002527 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002528
2529 /* Assume that the context is filled with NULL pointer
2530 * before the first call.
2531 * smp->ctx.a[2] = NULL;
2532 * smp->ctx.a[3] = NULL;
2533 */
2534 }
2535
2536 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2537}
2538
2539/* This function iterates over each parameter of the body. This requires
2540 * that the body has been waited for using http-buffer-request. It uses
2541 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2542 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2543 * optional second part if the body wraps at the end of the buffer. An optional
2544 * parameter name is passed in args[0], otherwise any parameter is considered.
2545 */
2546static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2547{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002548 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002549 const char *name;
2550 int name_len;
2551
2552 if (!args || (args[0].type && args[0].type != ARGT_STR))
2553 return 0;
2554
2555 name = "";
2556 name_len = 0;
2557 if (args[0].type == ARGT_STR) {
2558 name = args[0].data.str.area;
2559 name_len = args[0].data.str.data;
2560 }
2561
2562 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002563 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002564 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002565 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002566 struct buffer *temp;
2567 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002568
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002569 if (!htx)
2570 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002571
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002572 temp = get_trash_chunk();
2573 for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) {
2574 struct htx_blk *blk = htx_get_blk(htx, pos);
2575 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002576
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002577 if (type == HTX_BLK_EOM || type == HTX_BLK_EOD)
2578 break;
2579 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01002580 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002581 return 0;
2582 }
2583 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002584
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002585 smp->ctx.a[0] = temp->area;
2586 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002587
2588 /* Assume that the context is filled with NULL pointer
2589 * before the first call.
2590 * smp->ctx.a[2] = NULL;
2591 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002592 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002593 }
2594 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002595 /* LEGACY version */
2596 struct http_msg *msg;
2597 unsigned long len;
2598 unsigned long block1;
2599 char *body;
2600
Christopher Faulet89dc4992019-04-17 12:02:59 +02002601 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002602
Christopher Faulet89dc4992019-04-17 12:02:59 +02002603 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002604 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +02002605 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002606
2607 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002608 if (block1 > b_wrap(&chn->buf) - body)
2609 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002610
2611 if (block1 == len) {
2612 /* buffer is not wrapped (or empty) */
2613 smp->ctx.a[0] = body;
2614 smp->ctx.a[1] = body + len;
2615
2616 /* Assume that the context is filled with NULL pointer
2617 * before the first call.
2618 * smp->ctx.a[2] = NULL;
2619 * smp->ctx.a[3] = NULL;
2620 */
2621 }
2622 else {
2623 /* buffer is wrapped, we need to defragment it */
2624 smp->ctx.a[0] = body;
2625 smp->ctx.a[1] = body + block1;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002626 smp->ctx.a[2] = b_orig(&chn->buf);
2627 smp->ctx.a[3] = b_orig(&chn->buf) + ( len - block1 );
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002628 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002629 }
2630 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002631
Willy Tarreau79e57332018-10-02 16:01:16 +02002632 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2633}
2634
2635/* Return the signed integer value for the specified url parameter (see url_param
2636 * above).
2637 */
2638static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2639{
2640 int ret = smp_fetch_url_param(args, smp, kw, private);
2641
2642 if (ret > 0) {
2643 smp->data.type = SMP_T_SINT;
2644 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2645 smp->data.u.str.data);
2646 }
2647
2648 return ret;
2649}
2650
2651/* This produces a 32-bit hash of the concatenation of the first occurrence of
2652 * the Host header followed by the path component if it begins with a slash ('/').
2653 * This means that '*' will not be added, resulting in exactly the first Host
2654 * entry. If no Host header is found, then the path is used. The resulting value
2655 * is hashed using the url hash followed by a full avalanche hash and provides a
2656 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2657 * high-traffic sites without having to store whole paths.
2658 * this differs from the base32 functions in that it includes the url parameters
2659 * as well as the path
2660 */
2661static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2662{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002663 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002664 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002665
Christopher Faulet46575cd2019-04-17 11:40:30 +02002666 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002667 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002668 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002669 struct http_hdr_ctx ctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002670 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002671 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002672
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002673 if (!htx)
2674 return 0;
2675
2676 ctx.blk = NULL;
2677 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2678 /* OK we have the header value in ctx.value */
2679 while (ctx.value.len--)
2680 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2681 }
2682
2683 /* now retrieve the path */
2684 sl = http_find_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002685 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002686 while (path.len > 0 && *(path.ptr) != '?') {
2687 path.ptr++;
2688 path.len--;
2689 }
2690 if (path.len && *(path.ptr) == '/') {
2691 while (path.len--)
2692 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2693 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002694 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002695 else {
2696 /* LEGACY version */
2697 struct http_txn *txn;
2698 struct hdr_ctx ctx;
2699 char *ptr, *beg, *end;
2700 int len;
2701
Christopher Faulet89dc4992019-04-17 12:02:59 +02002702 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002703
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002704 txn = smp->strm->txn;
2705 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002706 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002707 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2708 ptr = ctx.line + ctx.val;
2709 len = ctx.vlen;
2710 while (len--)
2711 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2712 }
2713
2714 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02002715 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002716 beg = http_txn_get_path(txn);
2717 if (!beg)
2718 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002719
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002720 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002721
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002722 if (beg < ptr && *beg == '/') {
2723 while (beg < ptr)
2724 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2725 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002726 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002727
Willy Tarreau79e57332018-10-02 16:01:16 +02002728 hash = full_hash(hash);
2729
2730 smp->data.type = SMP_T_SINT;
2731 smp->data.u.sint = hash;
2732 smp->flags = SMP_F_VOL_1ST;
2733 return 1;
2734}
2735
2736/* This concatenates the source address with the 32-bit hash of the Host and
2737 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2738 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2739 * on the source address length. The URL hash is stored before the address so
2740 * that in environments where IPv6 is insignificant, truncating the output to
2741 * 8 bytes would still work.
2742 */
2743static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2744{
2745 struct buffer *temp;
2746 struct connection *cli_conn = objt_conn(smp->sess->origin);
2747
2748 if (!cli_conn)
2749 return 0;
2750
2751 if (!smp_fetch_url32(args, smp, kw, private))
2752 return 0;
2753
2754 temp = get_trash_chunk();
2755 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2756 temp->data += sizeof(unsigned int);
2757
2758 switch (cli_conn->addr.from.ss_family) {
2759 case AF_INET:
2760 memcpy(temp->area + temp->data,
2761 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2762 4);
2763 temp->data += 4;
2764 break;
2765 case AF_INET6:
2766 memcpy(temp->area + temp->data,
2767 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2768 16);
2769 temp->data += 16;
2770 break;
2771 default:
2772 return 0;
2773 }
2774
2775 smp->data.u.str = *temp;
2776 smp->data.type = SMP_T_BIN;
2777 return 1;
2778}
2779
2780/************************************************************************/
2781/* Other utility functions */
2782/************************************************************************/
2783
2784/* This function is used to validate the arguments passed to any "hdr" fetch
2785 * keyword. These keywords support an optional positive or negative occurrence
2786 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2787 * is assumed that the types are already the correct ones. Returns 0 on error,
2788 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2789 * error message in case of error, that the caller is responsible for freeing.
2790 * The initial location must either be freeable or NULL.
2791 * Note: this function's pointer is checked from Lua.
2792 */
2793int val_hdr(struct arg *arg, char **err_msg)
2794{
2795 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2796 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2797 return 0;
2798 }
2799 return 1;
2800}
2801
2802/************************************************************************/
2803/* All supported sample fetch keywords must be declared here. */
2804/************************************************************************/
2805
2806/* Note: must not be declared <const> as its list will be overwritten */
2807static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2808 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2809 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2810 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2811
2812 /* capture are allocated and are permanent in the stream */
2813 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2814
2815 /* retrieve these captures from the HTTP logs */
2816 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2817 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2818 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2819
2820 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2821 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2822
2823 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2824 * are only here to match the ACL's name, are request-only and are used
2825 * for ACL compatibility only.
2826 */
2827 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2828 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
2829 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2830 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2831
2832 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2833 * only here to match the ACL's name, are request-only and are used for
2834 * ACL compatibility only.
2835 */
2836 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
2837 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2838 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2839 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2840
2841 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2842 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2843 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2844 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2845 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2846 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2847
2848 /* HTTP protocol on the request path */
2849 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2850 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2851
2852 /* HTTP version on the request path */
2853 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2854 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2855
2856 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2857 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2858 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2859 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2860
2861 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2862 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2863
2864 /* HTTP version on the response path */
2865 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2866 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2867
2868 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2869 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2870 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2871 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2872
2873 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2874 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2875 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2876 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2877 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2878 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2879 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2880
2881 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2882 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2883 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2884 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2885
2886 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2887 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2888 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2889 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2890 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2891 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2892 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2893
2894 /* scook is valid only on the response and is used for ACL compatibility */
2895 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2896 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2897 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2898 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2899
2900 /* shdr is valid only on the response and is used for ACL compatibility */
2901 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2902 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2903 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2904 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2905
2906 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2907 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2908 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2909 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2910 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2911 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2912 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2913 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2914 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2915 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2916 { /* END */ },
2917}};
2918
Willy Tarreau0108d902018-11-25 19:14:37 +01002919INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002920
2921/*
2922 * Local variables:
2923 * c-indent-level: 8
2924 * c-basic-offset: 8
2925 * End:
2926 */