blob: 67ea2094ca5d43596ce46bb7f125c9ee2ac89883 [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 Faulete98411b2019-07-15 13:58:29 +020062static int get_http_auth(struct sample *smp, struct htx *htx)
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 Faulete98411b2019-07-15 13:58:29 +020078 if (htx) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +020079 /* HTX version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +020080 struct http_hdr_ctx ctx = { .blk = NULL };
81 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020082
Christopher Faulet311c7ea2018-10-24 21:41:55 +020083 if (txn->flags & TX_USE_PX_CONN)
84 hdr = ist("Proxy-Authorization");
85 else
86 hdr = ist("Authorization");
87
Christopher Faulet311c7ea2018-10-24 21:41:55 +020088 ctx.blk = NULL;
89 if (!http_find_header(htx, hdr, &ctx, 0))
90 return 0;
91
92 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
93 len = p - ctx.value.ptr;
94 if (!p || len <= 0)
95 return 0;
96
97 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
98 return 0;
99
100 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200101 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200102 else {
103 /* LEGACY version */
104 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau79e57332018-10-02 16:01:16 +0200105
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200106 if (txn->flags & TX_USE_PX_CONN) {
107 h = "Proxy-Authorization";
108 len = strlen(h);
109 } else {
110 h = "Authorization";
111 len = strlen(h);
112 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200113
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200114 if (!http_find_header2(h, len, ci_head(&s->req), &txn->hdr_idx, &ctx))
115 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200116
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200117 h = ctx.line + ctx.val;
Willy Tarreau79e57332018-10-02 16:01:16 +0200118
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200119 p = memchr(h, ' ', ctx.vlen);
120 len = p - h;
121 if (!p || len <= 0)
122 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200123
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200124 if (chunk_initlen(&auth_method, h, 0, len) != 1)
125 return 0;
126
127 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
128 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200129
130 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
131 struct buffer *http_auth = get_trash_chunk();
132
133 len = base64dec(txn->auth.method_data.area,
134 txn->auth.method_data.data,
135 http_auth->area, global.tune.bufsize - 1);
136
137 if (len < 0)
138 return 0;
139
140
141 http_auth->area[len] = '\0';
142
143 p = strchr(http_auth->area, ':');
144
145 if (!p)
146 return 0;
147
148 txn->auth.user = http_auth->area;
149 *p = '\0';
150 txn->auth.pass = p+1;
151
152 txn->auth.method = HTTP_AUTH_BASIC;
153 return 1;
154 }
155
156 return 0;
157}
158
159/* This function ensures that the prerequisites for an L7 fetch are ready,
160 * which means that a request or response is ready. If some data is missing,
161 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200162 * to extract data from L7. If <vol> is non-null during a prefetch, another
163 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200164 *
165 * The function returns :
166 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
167 * decide whether or not an HTTP message is present ;
168 * NULL if the requested data cannot be fetched or if it is certain that
169 * we'll never have any HTTP message there ;
170 * The HTX message if ready
171 */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200172struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200173{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200174 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200175 struct http_txn *txn = NULL;
176 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200177 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100178 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200179
180 /* Note: it is possible that <s> is NULL when called before stream
181 * initialization (eg: tcp-request connection), so this function is the
182 * one responsible for guarding against this case for all HTTP users.
183 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200184 if (!s || !chn)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200185 return NULL;
186
187 if (!s->txn) {
188 if (unlikely(!http_alloc_txn(s)))
189 return NULL; /* not enough memory */
190 http_init_txn(s);
191 txn = s->txn;
192 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200193 txn = s->txn;
194 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
195 smp->data.type = SMP_T_BOOL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200196
Christopher Fauleteca88542019-04-03 10:12:42 +0200197 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200198 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200199
Christopher Faulet89dc4992019-04-17 12:02:59 +0200200 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
201 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200202
Christopher Faulet89dc4992019-04-17 12:02:59 +0200203 if (msg->msg_state < HTTP_MSG_BODY) {
204 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200205 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200206 /* Parsing is done by the mux, just wait */
207 smp->flags |= SMP_F_MAY_CHANGE;
208 return NULL;
209 }
210 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200211 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200212 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200213 /* The start-line was already forwarded, it is too late to fetch anything */
214 return NULL;
215 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200216 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200217 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200218 struct buffer *buf;
219 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200220 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200221 union h1_sl h1sl;
222 unsigned int flags = HTX_FL_NONE;
223 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200224
Christopher Faulet89dc4992019-04-17 12:02:59 +0200225 /* no HTTP fetch on the response in TCP mode */
226 if (chn->flags & CF_ISRESP)
227 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200228
Christopher Faulet89dc4992019-04-17 12:02:59 +0200229 /* Now we are working on the request only */
230 buf = &chn->buf;
231 if (b_head(buf) + b_data(buf) > b_wrap(buf))
232 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200233
Christopher Faulet89dc4992019-04-17 12:02:59 +0200234 h1m_init_req(&h1m);
235 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
236 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
237 if (ret <= 0) {
238 /* Invalid or too big*/
239 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200240 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100241
Christopher Faulet89dc4992019-04-17 12:02:59 +0200242 /* wait for a full request */
243 smp->flags |= SMP_F_MAY_CHANGE;
244 return NULL;
245 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100246
Christopher Faulet89dc4992019-04-17 12:02:59 +0200247 /* OK we just got a valid HTTP mesage. We have to convert it
248 * into an HTX message.
249 */
250 if (unlikely(h1sl.rq.v.len == 0)) {
251 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
252 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200253 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200254 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200255 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200256
257 /* Set HTX start-line flags */
258 if (h1m.flags & H1_MF_VER_11)
259 flags |= HTX_SL_F_VER_11;
260 if (h1m.flags & H1_MF_XFER_ENC)
261 flags |= HTX_SL_F_XFER_ENC;
262 flags |= HTX_SL_F_XFER_LEN;
263 if (h1m.flags & H1_MF_CHNK)
264 flags |= HTX_SL_F_CHNK;
265 else if (h1m.flags & H1_MF_CLEN)
266 flags |= HTX_SL_F_CLEN;
267
268 htx = htx_from_buf(get_trash_chunk());
269 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
270 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200271 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200272 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200273 }
274
275 /* OK we just got a valid HTTP message. If not already done by
276 * HTTP analyzers, we have some minor preparation to perform so
277 * that further checks can rely on HTTP tests.
278 */
279 if (sl && msg->msg_state < HTTP_MSG_BODY) {
280 if (!(chn->flags & CF_ISRESP)) {
281 txn->meth = sl->info.req.meth;
282 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
283 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200284 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200285 else
286 txn->status = sl->info.res.status;
287 if (sl->flags & HTX_SL_F_VER_11)
288 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200289 }
290
291 /* everything's OK */
292 smp->data.u.sint = 1;
293 return htx;
294}
295
296/* This function ensures that the prerequisites for an L7 fetch are ready,
297 * which means that a request or response is ready. If some data is missing,
298 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau79e57332018-10-02 16:01:16 +0200299 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
300 * another test is made to ensure the required information is not gone.
301 *
302 * The function returns :
303 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
304 * decide whether or not an HTTP message is present ;
305 * 0 if the requested data cannot be fetched or if it is certain that
306 * we'll never have any HTTP message there ;
307 * 1 if an HTTP message is ready
308 */
309int smp_prefetch_http(struct proxy *px, struct stream *s, unsigned int opt,
Christopher Faulet89dc4992019-04-17 12:02:59 +0200310 struct channel *chn, struct sample *smp, int req_vol)
Willy Tarreau79e57332018-10-02 16:01:16 +0200311{
312 struct http_txn *txn;
313 struct http_msg *msg;
314
315 /* Note: it is possible that <s> is NULL when called before stream
316 * initialization (eg: tcp-request connection), so this function is the
317 * one responsible for guarding against this case for all HTTP users.
318 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200319 if (!s || !chn)
Willy Tarreau79e57332018-10-02 16:01:16 +0200320 return 0;
321
322 if (!s->txn) {
323 if (unlikely(!http_alloc_txn(s)))
324 return 0; /* not enough memory */
325 http_init_txn(s);
326 }
327 txn = s->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200328 smp->data.type = SMP_T_BOOL;
329
Christopher Faulet89dc4992019-04-17 12:02:59 +0200330 if (chn->flags & CF_ISRESP) {
331 /* Check for a dependency on a response */
332 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
333 smp->flags |= SMP_F_MAY_CHANGE;
334 return 0;
335 }
336 goto end;
337 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200338
Christopher Faulet89dc4992019-04-17 12:02:59 +0200339 /* Check for a dependency on a request */
340 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200341
Christopher Faulet89dc4992019-04-17 12:02:59 +0200342 if (req_vol && (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
343 return 0; /* data might have moved and indexes changed */
344 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200345
Christopher Faulet89dc4992019-04-17 12:02:59 +0200346 /* If the buffer does not leave enough free space at the end, we must
347 * first realign it.
348 */
349 if (ci_head(chn) > b_orig(&chn->buf) &&
350 ci_head(chn) + ci_data(chn) > b_wrap(&chn->buf) - global.tune.maxrewrite)
351 channel_slow_realign(chn, trash.area);
Willy Tarreau79e57332018-10-02 16:01:16 +0200352
Christopher Faulet89dc4992019-04-17 12:02:59 +0200353 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
354 if (msg->msg_state == HTTP_MSG_ERROR)
355 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200356
Christopher Faulet89dc4992019-04-17 12:02:59 +0200357 /* Try to decode HTTP request */
358 if (likely(msg->next < ci_data(chn)))
359 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau79e57332018-10-02 16:01:16 +0200360
Christopher Faulet89dc4992019-04-17 12:02:59 +0200361 /* Still no valid request ? */
362 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
363 if ((msg->msg_state == HTTP_MSG_ERROR) ||
364 channel_full(chn, global.tune.maxrewrite)) {
Willy Tarreau79e57332018-10-02 16:01:16 +0200365 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200366 }
367 /* wait for final state */
368 smp->flags |= SMP_F_MAY_CHANGE;
369 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200370 }
371
Christopher Faulet89dc4992019-04-17 12:02:59 +0200372 /* OK we just got a valid HTTP message. We have some minor
373 * preparation to perform so that further checks can rely
374 * on HTTP tests.
375 */
376
377 /* If the message was parsed but was too large, we must absolutely
378 * return an error so that it is not processed. At the moment this
379 * cannot happen, but if the parsers are to change in the future,
380 * we want this check to be maintained.
381 */
382 if (unlikely(ci_head(chn) + ci_data(chn) >
383 b_wrap(&chn->buf) - global.tune.maxrewrite)) {
384 msg->err_state = msg->msg_state;
385 msg->msg_state = HTTP_MSG_ERROR;
386 smp->data.u.sint = 1;
387 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200388 }
389
Christopher Faulet89dc4992019-04-17 12:02:59 +0200390 txn->meth = find_http_meth(ci_head(chn), msg->sl.rq.m_l);
391 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
392 s->flags |= SF_REDIRECTABLE;
393
394 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau79e57332018-10-02 16:01:16 +0200395 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200396 }
397
Christopher Faulet89dc4992019-04-17 12:02:59 +0200398 end:
Willy Tarreau79e57332018-10-02 16:01:16 +0200399 /* everything's OK */
400 smp->data.u.sint = 1;
401 return 1;
402}
403
404/* This function fetches the method of current HTTP request and stores
405 * it in the global pattern struct as a chunk. There are two possibilities :
406 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
407 * in <len> and <ptr> is NULL ;
408 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
409 * <len> to its length.
410 * This is intended to be used with pat_match_meth() only.
411 */
412static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
413{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200414 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200415 int meth;
416 struct http_txn *txn;
417
Christopher Faulet46575cd2019-04-17 11:40:30 +0200418 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200419 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200420 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +0200421
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200422 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +0200423 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200424
425 txn = smp->strm->txn;
426 meth = txn->meth;
427 smp->data.type = SMP_T_METH;
428 smp->data.u.meth.meth = meth;
429 if (meth == HTTP_METH_OTHER) {
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100430 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200431
Christopher Faulet89dc4992019-04-17 12:02:59 +0200432 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200433 /* ensure the indexes are not affected */
434 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200435 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200436 sl = http_get_stline(htx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200437 smp->flags |= SMP_F_CONST;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100438 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
439 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200440 }
441 smp->flags |= SMP_F_VOL_1ST;
442 }
443 else {
444 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200445 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200446
447 txn = smp->strm->txn;
448 meth = txn->meth;
449 smp->data.type = SMP_T_METH;
450 smp->data.u.meth.meth = meth;
451 if (meth == HTTP_METH_OTHER) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200452 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200453 /* ensure the indexes are not affected */
454 return 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200455 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200456 smp->flags |= SMP_F_CONST;
457 smp->data.u.meth.str.data = txn->req.sl.rq.m_l;
458 smp->data.u.meth.str.area = ci_head(txn->req.chn);
459 }
460 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200461 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200462 return 1;
463}
464
465static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
466{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200467 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200468 struct http_txn *txn;
469 char *ptr;
470 int len;
471
Christopher Faulet46575cd2019-04-17 11:40:30 +0200472 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200473 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200474 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100475 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200476
477 if (!htx)
478 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200479
Christopher Faulet297fbb42019-05-13 14:41:27 +0200480 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100481 len = HTX_SL_REQ_VLEN(sl);
482 ptr = HTX_SL_REQ_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200483 }
484 else {
485 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200486 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200487
488 txn = smp->strm->txn;
489 len = txn->req.sl.rq.v_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200490 ptr = ci_head(chn) + txn->req.sl.rq.v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200491 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200492
493 while ((len-- > 0) && (*ptr++ != '/'));
494 if (len <= 0)
495 return 0;
496
497 smp->data.type = SMP_T_STR;
498 smp->data.u.str.area = ptr;
499 smp->data.u.str.data = len;
500
501 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
502 return 1;
503}
504
505static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
506{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200507 struct channel *chn = SMP_RES_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200508 struct http_txn *txn;
509 char *ptr;
510 int len;
511
Christopher Faulet46575cd2019-04-17 11:40:30 +0200512 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200513 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200514 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100515 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200516
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200517 if (!htx)
518 return 0;
519
Christopher Faulet297fbb42019-05-13 14:41:27 +0200520 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100521 len = HTX_SL_RES_VLEN(sl);
522 ptr = HTX_SL_RES_VPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200523 }
524 else {
525 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200526 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200527
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200528 txn = smp->strm->txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200529 len = txn->rsp.sl.st.v_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200530 ptr = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200531 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200532
533 while ((len-- > 0) && (*ptr++ != '/'));
534 if (len <= 0)
535 return 0;
536
537 smp->data.type = SMP_T_STR;
538 smp->data.u.str.area = ptr;
539 smp->data.u.str.data = len;
540
541 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
542 return 1;
543}
544
545/* 3. Check on Status Code. We manipulate integers here. */
546static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
547{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200548 struct channel *chn = SMP_RES_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200549 struct http_txn *txn;
550 char *ptr;
551 int len;
552
Christopher Faulet46575cd2019-04-17 11:40:30 +0200553 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200554 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200555 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100556 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200557
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200558 if (!htx)
559 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200560
Christopher Faulet297fbb42019-05-13 14:41:27 +0200561 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100562 len = HTX_SL_RES_CLEN(sl);
563 ptr = HTX_SL_RES_CPTR(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200564 }
565 else {
566 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200567 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200568
569 txn = smp->strm->txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200570 len = txn->rsp.sl.st.c_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200571 ptr = ci_head(chn) + txn->rsp.sl.st.c;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200572 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200573
574 smp->data.type = SMP_T_SINT;
575 smp->data.u.sint = __strl2ui(ptr, len);
576 smp->flags = SMP_F_VOL_1ST;
577 return 1;
578}
579
580static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
581{
582 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
583 return 0;
584
585 if (!smp->strm->unique_id) {
586 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
587 return 0;
588 smp->strm->unique_id[0] = '\0';
589 }
590 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
591 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
592
593 smp->data.type = SMP_T_STR;
594 smp->data.u.str.area = smp->strm->unique_id;
595 smp->flags = SMP_F_CONST;
596 return 1;
597}
598
599/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800600 * empty line which separes headers from the body. This is useful
601 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200602 */
603static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
604{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200605 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200606 struct http_txn *txn;
607
Christopher Faulet46575cd2019-04-17 11:40:30 +0200608 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200609 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200610 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200611 struct buffer *temp;
612 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200613
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200614 if (!htx)
615 return 0;
616 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +0200617 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200618 struct htx_blk *blk = htx_get_blk(htx, pos);
619 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200620
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200621 if (type == HTX_BLK_HDR) {
622 struct ist n = htx_get_blk_name(htx, blk);
623 struct ist v = htx_get_blk_value(htx, blk);
624
Christopher Fauletc59ff232018-12-03 13:58:44 +0100625 if (!htx_hdr_to_h1(n, v, temp))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200626 return 0;
627 }
628 else if (type == HTX_BLK_EOH) {
629 if (!chunk_memcat(temp, "\r\n", 2))
630 return 0;
631 break;
632 }
633 }
634 smp->data.type = SMP_T_STR;
635 smp->data.u.str = *temp;
636
637 }
638 else {
639 /* LEGACY version */
640 struct http_msg *msg;
641 struct hdr_idx *idx;
642
Christopher Faulet89dc4992019-04-17 12:02:59 +0200643 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200644
645 txn = smp->strm->txn;
646 idx = &txn->hdr_idx;
647 msg = &txn->req;
Willy Tarreau79e57332018-10-02 16:01:16 +0200648
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200649 smp->data.type = SMP_T_STR;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200650 smp->data.u.str.area = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200651 smp->data.u.str.data = msg->eoh - hdr_idx_first_pos(idx) + 1 +
Christopher Faulet89dc4992019-04-17 12:02:59 +0200652 (ci_head(chn)[msg->eoh] == '\r');
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200653 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200654 return 1;
655}
656
657/* Returns the header request in a length/value encoded format.
658 * This is useful for exchanges with the SPOE.
659 *
660 * A "length value" is a multibyte code encoding numbers. It uses the
661 * SPOE format. The encoding is the following:
662 *
663 * Each couple "header name" / "header value" is composed
664 * like this:
665 * "length value" "header name bytes"
666 * "length value" "header value bytes"
667 * When the last header is reached, the header name and the header
668 * value are empty. Their length are 0
669 */
670static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
671{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200672 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200673 struct http_txn *txn;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200674 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200675
Christopher Faulet46575cd2019-04-17 11:40:30 +0200676 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200677 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200678 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200679 struct buffer *temp;
680 char *p, *end;
681 int32_t pos;
682 int ret;
Willy Tarreau79e57332018-10-02 16:01:16 +0200683
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200684 if (!htx)
685 return 0;
686 temp = get_trash_chunk();
687 p = temp->area;
688 end = temp->area + temp->size;
Christopher Fauleta3f15502019-05-13 15:27:23 +0200689 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200690 struct htx_blk *blk = htx_get_blk(htx, pos);
691 enum htx_blk_type type = htx_get_blk_type(blk);
692 struct ist n, v;
Willy Tarreau79e57332018-10-02 16:01:16 +0200693
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200694 if (type == HTX_BLK_HDR) {
695 n = htx_get_blk_name(htx,blk);
696 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200697
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200698 /* encode the header name. */
699 ret = encode_varint(n.len, &p, end);
700 if (ret == -1)
701 return 0;
702 if (p + n.len > end)
703 return 0;
704 memcpy(p, n.ptr, n.len);
705 p += n.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200706
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200707 /* encode the header value. */
708 ret = encode_varint(v.len, &p, end);
709 if (ret == -1)
710 return 0;
711 if (p + v.len > end)
712 return 0;
713 memcpy(p, v.ptr, v.len);
714 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200715
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200716 }
717 else if (type == HTX_BLK_EOH) {
718 /* encode the end of the header list with empty
719 * header name and header value.
720 */
721 ret = encode_varint(0, &p, end);
722 if (ret == -1)
723 return 0;
724 ret = encode_varint(0, &p, end);
725 if (ret == -1)
726 return 0;
727 break;
728 }
729 }
730
731 /* Initialise sample data which will be filled. */
732 smp->data.type = SMP_T_BIN;
733 smp->data.u.str.area = temp->area;
734 smp->data.u.str.data = p - temp->area;
735 smp->data.u.str.size = temp->size;
736 }
737 else {
738 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200739 struct hdr_idx *idx;
740 const char *cur_ptr, *cur_next, *p;
741 int old_idx, cur_idx;
742 struct hdr_idx_elem *cur_hdr;
743 const char *hn, *hv;
744 int hnl, hvl;
745 int ret;
746 char *buf;
747 char *end;
748
Christopher Faulet89dc4992019-04-17 12:02:59 +0200749 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200750
751 temp = get_trash_chunk();
752 buf = temp->area;
753 end = temp->area + temp->size;
754
755 txn = smp->strm->txn;
756 idx = &txn->hdr_idx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200757
758 /* Build array of headers. */
759 old_idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200760 cur_next = ci_head(chn) + hdr_idx_first_pos(idx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200761 while (1) {
762 cur_idx = idx->v[old_idx].next;
763 if (!cur_idx)
764 break;
765 old_idx = cur_idx;
Willy Tarreau79e57332018-10-02 16:01:16 +0200766
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200767 cur_hdr = &idx->v[cur_idx];
768 cur_ptr = cur_next;
769 cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1;
770
771 /* Now we have one full header at cur_ptr of len cur_hdr->len,
772 * and the next header starts at cur_next. We'll check
773 * this header in the list as well as against the default
774 * rule.
775 */
776
777 /* look for ': *'. */
778 hn = cur_ptr;
779 for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++);
780 if (p >= cur_ptr+cur_hdr->len)
781 continue;
782 hnl = p - hn;
Willy Tarreau79e57332018-10-02 16:01:16 +0200783 p++;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200784 while (p < cur_ptr + cur_hdr->len && (*p == ' ' || *p == '\t'))
785 p++;
786 if (p >= cur_ptr + cur_hdr->len)
787 continue;
788 hv = p;
789 hvl = cur_ptr + cur_hdr->len-p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200790
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200791 /* encode the header name. */
792 ret = encode_varint(hnl, &buf, end);
793 if (ret == -1)
794 return 0;
795 if (buf + hnl > end)
796 return 0;
797 memcpy(buf, hn, hnl);
798 buf += hnl;
799
800 /* encode and copy the value. */
801 ret = encode_varint(hvl, &buf, end);
802 if (ret == -1)
803 return 0;
804 if (buf + hvl > end)
805 return 0;
806 memcpy(buf, hv, hvl);
807 buf += hvl;
808 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200809
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200810 /* encode the end of the header list with empty
811 * header name and header value.
812 */
813 ret = encode_varint(0, &buf, end);
Willy Tarreau79e57332018-10-02 16:01:16 +0200814 if (ret == -1)
815 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200816 ret = encode_varint(0, &buf, end);
817 if (ret == -1)
Willy Tarreau79e57332018-10-02 16:01:16 +0200818 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200819
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200820 /* Initialise sample data which will be filled. */
821 smp->data.type = SMP_T_BIN;
822 smp->data.u.str.area = temp->area;
823 smp->data.u.str.data = buf - temp->area;
824 smp->data.u.str.size = temp->size;
825 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200826 return 1;
827}
828
829/* returns the longest available part of the body. This requires that the body
830 * has been waited for using http-buffer-request.
831 */
832static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
833{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200834 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200835 struct buffer *temp;
836
Christopher Faulet46575cd2019-04-17 11:40:30 +0200837 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200838 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200839 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200840 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200841
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200842 if (!htx)
843 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200844
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200845 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +0200846 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200847 struct htx_blk *blk = htx_get_blk(htx, pos);
848 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200849
Christopher Faulet54b5e212019-06-04 10:08:28 +0200850 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200851 break;
852 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +0100853 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200854 return 0;
855 }
856 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200857
Willy Tarreau79e57332018-10-02 16:01:16 +0200858 smp->data.type = SMP_T_BIN;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200859 smp->data.u.str = *temp;
860 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200861 }
862 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200863 /* LEGACY version */
864 struct http_msg *msg;
865 unsigned long len;
866 unsigned long block1;
867 char *body;
868
Christopher Faulet89dc4992019-04-17 12:02:59 +0200869 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200870
Christopher Faulet89dc4992019-04-17 12:02:59 +0200871 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200872 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200873 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200874
875 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200876 if (block1 > b_wrap(&chn->buf) - body)
877 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200878
879 if (block1 == len) {
880 /* buffer is not wrapped (or empty) */
881 smp->data.type = SMP_T_BIN;
882 smp->data.u.str.area = body;
883 smp->data.u.str.data = len;
884 smp->flags = SMP_F_VOL_TEST | SMP_F_CONST;
885 }
886 else {
887 /* buffer is wrapped, we need to defragment it */
888 temp = get_trash_chunk();
889 memcpy(temp->area, body, block1);
Christopher Faulet89dc4992019-04-17 12:02:59 +0200890 memcpy(temp->area + block1, b_orig(&chn->buf), len - block1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200891 smp->data.type = SMP_T_BIN;
892 smp->data.u.str.area = temp->area;
893 smp->data.u.str.data = len;
894 smp->flags = SMP_F_VOL_TEST;
895 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200896 }
897 return 1;
898}
899
900
901/* returns the available length of the body. This requires that the body
902 * has been waited for using http-buffer-request.
903 */
904static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
905{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200906 struct channel *chn = SMP_REQ_CHN(smp);
907
Christopher Faulet46575cd2019-04-17 11:40:30 +0200908 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200909 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200910 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +0100911 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100912 unsigned long long len = 0;
913
914 if (!htx)
915 return 0;
916
Christopher Fauleta3f15502019-05-13 15:27:23 +0200917 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Dragan Dosen5a606682019-02-14 12:30:53 +0100918 struct htx_blk *blk = htx_get_blk(htx, pos);
919 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100920
Christopher Faulet54b5e212019-06-04 10:08:28 +0200921 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletc16317d2018-12-12 14:11:22 +0100922 break;
Dragan Dosen5a606682019-02-14 12:30:53 +0100923 if (type == HTX_BLK_DATA)
924 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100925 }
926
927 smp->data.type = SMP_T_SINT;
928 smp->data.u.sint = len;
929
930 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200931 }
932 else {
933 /* LEGACY version */
934 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200935
Christopher Faulet89dc4992019-04-17 12:02:59 +0200936 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200937
Christopher Faulet89dc4992019-04-17 12:02:59 +0200938 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200939 smp->data.type = SMP_T_SINT;
940 smp->data.u.sint = http_body_bytes(msg);
Willy Tarreau79e57332018-10-02 16:01:16 +0200941
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200942 smp->flags = SMP_F_VOL_TEST;
943 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200944 return 1;
945}
946
947
948/* returns the advertised length of the body, or the advertised size of the
949 * chunks available in the buffer. This requires that the body has been waited
950 * for using http-buffer-request.
951 */
952static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
953{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200954 struct channel *chn = SMP_REQ_CHN(smp);
955
Christopher Faulet46575cd2019-04-17 11:40:30 +0200956 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200957 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200958 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Dragan Dosen5a606682019-02-14 12:30:53 +0100959 int32_t pos;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100960 unsigned long long len = 0;
961
962 if (!htx)
963 return 0;
964
Christopher Fauleta3f15502019-05-13 15:27:23 +0200965 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Dragan Dosen5a606682019-02-14 12:30:53 +0100966 struct htx_blk *blk = htx_get_blk(htx, pos);
967 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100968
Christopher Faulet54b5e212019-06-04 10:08:28 +0200969 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauletc16317d2018-12-12 14:11:22 +0100970 break;
Dragan Dosen5a606682019-02-14 12:30:53 +0100971 if (type == HTX_BLK_DATA)
972 len += htx_get_blksz(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100973 }
974 if (htx->extra != ULLONG_MAX)
975 len += htx->extra;
976
977 smp->data.type = SMP_T_SINT;
978 smp->data.u.sint = len;
979
980 smp->flags = SMP_F_VOL_TEST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200981 }
982 else {
983 /* LEGACY version */
984 struct http_msg *msg;
Willy Tarreau79e57332018-10-02 16:01:16 +0200985
Christopher Faulet89dc4992019-04-17 12:02:59 +0200986 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +0200987
Christopher Faulet89dc4992019-04-17 12:02:59 +0200988 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200989 smp->data.type = SMP_T_SINT;
990 smp->data.u.sint = msg->body_len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200991
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200992 smp->flags = SMP_F_VOL_TEST;
993 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200994 return 1;
995}
996
997
998/* 4. Check on URL/URI. A pointer to the URI is stored. */
999static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
1000{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001001 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001002 struct http_txn *txn;
1003
Christopher Faulet46575cd2019-04-17 11:40:30 +02001004 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001005 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001006 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001007 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001008
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001009 if (!htx)
1010 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001011 sl = http_get_stline(htx);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001012 smp->data.type = SMP_T_STR;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001013 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
1014 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001015 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1016 }
1017 else {
1018 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001019 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001020
1021 txn = smp->strm->txn;
1022 smp->data.type = SMP_T_STR;
1023 smp->data.u.str.data = txn->req.sl.rq.u_l;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001024 smp->data.u.str.area = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001025 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1026 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001027 return 1;
1028}
1029
1030static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1031{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001032 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001033 struct http_txn *txn;
1034 struct sockaddr_storage addr;
1035
Christopher Faulet46575cd2019-04-17 11:40:30 +02001036 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001037 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001038 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001039 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001040
1041 if (!htx)
1042 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001043 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001044 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001045 }
1046 else {
1047 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001048 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001049
1050 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001051 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001052 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001053
Willy Tarreau79e57332018-10-02 16:01:16 +02001054 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1055 return 0;
1056
1057 smp->data.type = SMP_T_IPV4;
1058 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
1059 smp->flags = 0;
1060 return 1;
1061}
1062
1063static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
1064{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001065 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001066 struct http_txn *txn;
1067 struct sockaddr_storage addr;
1068
Christopher Faulet46575cd2019-04-17 11:40:30 +02001069 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001070 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001071 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001072 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001073
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001074 if (!htx)
1075 return 0;
Christopher Faulet297fbb42019-05-13 14:41:27 +02001076 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001077 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001078 }
1079 else {
1080 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001081 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001082
1083 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001084 url2sa(ci_head(chn) + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001085 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001086 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
1087 return 0;
1088
1089 smp->data.type = SMP_T_SINT;
1090 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
1091 smp->flags = 0;
1092 return 1;
1093}
1094
1095/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1096 * Accepts an optional argument of type string containing the header field name,
1097 * and an optional argument of type signed or unsigned integer to request an
1098 * explicit occurrence of the header. Note that in the event of a missing name,
1099 * headers are considered from the first one. It does not stop on commas and
1100 * returns full lines instead (useful for User-Agent or Date for example).
1101 */
1102static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1103{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001104 /* possible keywords: req.fhdr, res.fhdr */
1105 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001106 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001107
Christopher Faulet46575cd2019-04-17 11:40:30 +02001108 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001109 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001110 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001111 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1112 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +02001113
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001114 if (!ctx) {
1115 /* first call */
1116 ctx = &static_http_hdr_ctx;
1117 ctx->blk = NULL;
1118 smp->ctx.a[0] = ctx;
1119 }
1120
1121 if (args) {
1122 if (args[0].type != ARGT_STR)
1123 return 0;
1124 name.ptr = args[0].data.str.area;
1125 name.len = args[0].data.str.data;
1126
1127 if (args[1].type == ARGT_SINT)
1128 occ = args[1].data.sint;
1129 }
1130
1131 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001132 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001133
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001134 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1135 /* search for header from the beginning */
1136 ctx->blk = NULL;
1137
1138 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1139 /* no explicit occurrence and single fetch => last header by default */
1140 occ = -1;
1141
1142 if (!occ)
1143 /* prepare to report multiple occurrences for ACL fetches */
1144 smp->flags |= SMP_F_NOT_LAST;
1145
1146 smp->data.type = SMP_T_STR;
1147 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1148 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1149 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001150 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001151 else {
1152 /* LEGACY version */
1153 struct hdr_idx *idx;
1154 struct hdr_ctx *ctx = smp->ctx.a[0];
1155 const struct http_msg *msg;
1156 const char *name_str = NULL;
1157 int name_len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001158
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001159 if (!ctx) {
1160 /* first call */
1161 ctx = &static_hdr_ctx;
1162 ctx->idx = 0;
1163 smp->ctx.a[0] = ctx;
1164 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001165
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001166 if (args) {
1167 if (args[0].type != ARGT_STR)
1168 return 0;
1169 name_str = args[0].data.str.area;
1170 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001171
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001172 if (args[1].type == ARGT_SINT)
1173 occ = args[1].data.sint;
1174 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001175
Christopher Faulet89dc4992019-04-17 12:02:59 +02001176 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001177
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001178 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001179 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001180
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001181 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1182 /* search for header from the beginning */
1183 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001184
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001185 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1186 /* no explicit occurrence and single fetch => last header by default */
1187 occ = -1;
1188
1189 if (!occ)
1190 /* prepare to report multiple occurrences for ACL fetches */
1191 smp->flags |= SMP_F_NOT_LAST;
1192
1193 smp->data.type = SMP_T_STR;
1194 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1195 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1196 return 1;
1197 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001198 smp->flags &= ~SMP_F_NOT_LAST;
1199 return 0;
1200}
1201
1202/* 6. Check on HTTP header count. The number of occurrences is returned.
1203 * Accepts exactly 1 argument of type string. It does not stop on commas and
1204 * returns full lines instead (useful for User-Agent or Date for example).
1205 */
1206static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1207{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001208 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
1209 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001210 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001211
Christopher Faulet46575cd2019-04-17 11:40:30 +02001212 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001213 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001214 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001215 struct http_hdr_ctx ctx;
1216 struct ist name;
1217
1218 if (!htx)
1219 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001220
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001221 if (args && args->type == ARGT_STR) {
1222 name.ptr = args->data.str.area;
1223 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001224 } else {
1225 name.ptr = NULL;
1226 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001227 }
1228
1229 ctx.blk = NULL;
1230 cnt = 0;
1231 while (http_find_header(htx, name, &ctx, 1))
1232 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001233 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001234 else {
1235 /* LEGACY version */
1236 struct hdr_idx *idx;
1237 struct hdr_ctx ctx;
1238 const struct http_msg *msg;
1239 const char *name = NULL;
1240 int len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001241
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001242 if (args && args->type == ARGT_STR) {
1243 name = args->data.str.area;
1244 len = args->data.str.data;
1245 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001246
Christopher Faulet89dc4992019-04-17 12:02:59 +02001247 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001248
1249 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001250 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001251
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001252 ctx.idx = 0;
1253 cnt = 0;
1254 while (http_find_full_header2(name, len, ci_head(msg->chn), idx, &ctx))
1255 cnt++;
1256 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001257
1258 smp->data.type = SMP_T_SINT;
1259 smp->data.u.sint = cnt;
1260 smp->flags = SMP_F_VOL_HDR;
1261 return 1;
1262}
1263
1264static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
1265{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001266 /* possible keywords: req.hdr_names, res.hdr_names */
1267 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001268 struct buffer *temp;
1269 char del = ',';
1270
Christopher Faulet46575cd2019-04-17 11:40:30 +02001271 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001272 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001273 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001274 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001275
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001276 if (!htx)
1277 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001278
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001279 if (args && args->type == ARGT_STR)
1280 del = *args[0].data.str.area;
Willy Tarreau79e57332018-10-02 16:01:16 +02001281
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001282 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +02001283 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001284 struct htx_blk *blk = htx_get_blk(htx, pos);
1285 enum htx_blk_type type = htx_get_blk_type(blk);
1286 struct ist n;
Willy Tarreau79e57332018-10-02 16:01:16 +02001287
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001288 if (type == HTX_BLK_EOH)
1289 break;
1290 if (type != HTX_BLK_HDR)
1291 continue;
1292 n = htx_get_blk_name(htx, blk);
1293
1294 if (temp->data)
1295 temp->area[temp->data++] = del;
1296 chunk_memcat(temp, n.ptr, n.len);
1297 }
1298 }
1299 else {
1300 /* LEGACY version */
1301 struct hdr_idx *idx;
1302 struct hdr_ctx ctx;
1303 const struct http_msg *msg;
1304
1305 if (args && args->type == ARGT_STR)
1306 del = *args[0].data.str.area;
1307
Christopher Faulet89dc4992019-04-17 12:02:59 +02001308 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001309
1310 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001311 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001312
1313 temp = get_trash_chunk();
1314
1315 ctx.idx = 0;
1316 while (http_find_next_header(ci_head(msg->chn), idx, &ctx)) {
1317 if (temp->data)
1318 temp->area[temp->data++] = del;
1319 memcpy(temp->area + temp->data, ctx.line, ctx.del);
1320 temp->data += ctx.del;
1321 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001322 }
1323
1324 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001325 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001326 smp->flags = SMP_F_VOL_HDR;
1327 return 1;
1328}
1329
1330/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
1331 * Accepts an optional argument of type string containing the header field name,
1332 * and an optional argument of type signed or unsigned integer to request an
1333 * explicit occurrence of the header. Note that in the event of a missing name,
1334 * headers are considered from the first one.
1335 */
1336static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1337{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001338 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
1339 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001340 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001341
Christopher Faulet46575cd2019-04-17 11:40:30 +02001342 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001343 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001344 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001345 struct http_hdr_ctx *ctx = smp->ctx.a[0];
1346 struct ist name;
1347
1348 if (!ctx) {
1349 /* first call */
1350 ctx = &static_http_hdr_ctx;
1351 ctx->blk = NULL;
1352 smp->ctx.a[0] = ctx;
1353 }
1354
1355 if (args) {
1356 if (args[0].type != ARGT_STR)
1357 return 0;
1358 name.ptr = args[0].data.str.area;
1359 name.len = args[0].data.str.data;
1360
1361 if (args[1].type == ARGT_SINT)
1362 occ = args[1].data.sint;
1363 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001364
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001365 if (!htx)
Willy Tarreau79e57332018-10-02 16:01:16 +02001366 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001367
1368 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1369 /* search for header from the beginning */
1370 ctx->blk = NULL;
1371
1372 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1373 /* no explicit occurrence and single fetch => last header by default */
1374 occ = -1;
1375
1376 if (!occ)
1377 /* prepare to report multiple occurrences for ACL fetches */
1378 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001379
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001380 smp->data.type = SMP_T_STR;
1381 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1382 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1383 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001384 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001385 else {
1386 /* LEGACY version */
1387 struct hdr_idx *idx;
1388 struct hdr_ctx *ctx = smp->ctx.a[0];
1389 const struct http_msg *msg;
1390 const char *name_str = NULL;
1391 int name_len = 0;
1392
1393 if (!ctx) {
1394 /* first call */
1395 ctx = &static_hdr_ctx;
1396 ctx->idx = 0;
1397 smp->ctx.a[0] = ctx;
1398 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001399
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001400 if (args) {
1401 if (args[0].type != ARGT_STR)
1402 return 0;
1403 name_str = args[0].data.str.area;
1404 name_len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +02001405
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001406 if (args[1].type == ARGT_SINT)
1407 occ = args[1].data.sint;
1408 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001409
Christopher Faulet89dc4992019-04-17 12:02:59 +02001410 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001411
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001412 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001413 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001414
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001415 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
1416 /* search for header from the beginning */
1417 ctx->idx = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001418
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001419 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1420 /* no explicit occurrence and single fetch => last header by default */
1421 occ = -1;
1422
1423 if (!occ)
1424 /* prepare to report multiple occurrences for ACL fetches */
1425 smp->flags |= SMP_F_NOT_LAST;
1426
1427 smp->data.type = SMP_T_STR;
1428 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
1429 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
1430 return 1;
1431 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001432
1433 smp->flags &= ~SMP_F_NOT_LAST;
1434 return 0;
1435}
1436
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001437/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
1438 * the right channel. So instead of duplicating the code, we just change the
1439 * keyword and then fallback on smp_fetch_hdr().
1440 */
1441static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1442{
1443 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
1444 return smp_fetch_hdr(args, smp, kw, private);
1445}
1446
Willy Tarreau79e57332018-10-02 16:01:16 +02001447/* 6. Check on HTTP header count. The number of occurrences is returned.
1448 * Accepts exactly 1 argument of type string.
1449 */
1450static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1451{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001452 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
1453 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02001454 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001455
Christopher Faulet46575cd2019-04-17 11:40:30 +02001456 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001457 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001458 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001459 struct http_hdr_ctx ctx;
1460 struct ist name;
1461
1462 if (!htx)
1463 return 0;
1464
1465 if (args && args->type == ARGT_STR) {
1466 name.ptr = args->data.str.area;
1467 name.len = args->data.str.data;
Olivier Houcharde2c78cd2018-11-21 13:49:48 +01001468 } else {
1469 name.ptr = NULL;
1470 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001471 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001472
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001473 ctx.blk = NULL;
1474 cnt = 0;
1475 while (http_find_header(htx, name, &ctx, 0))
1476 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001477 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001478 else {
1479 /* LEGACY version */
1480 struct hdr_idx *idx;
1481 struct hdr_ctx ctx;
1482 const struct http_msg *msg;
1483 const char *name = NULL;
1484 int len = 0;
1485
1486 if (args && args->type == ARGT_STR) {
1487 name = args->data.str.area;
1488 len = args->data.str.data;
1489 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001490
Christopher Faulet89dc4992019-04-17 12:02:59 +02001491 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001492
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001493 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001494 msg = (!(chn->flags & CF_ISRESP) ? &smp->strm->txn->req : &smp->strm->txn->rsp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001495
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001496 ctx.idx = 0;
1497 cnt = 0;
1498 while (http_find_header2(name, len, ci_head(msg->chn), idx, &ctx))
1499 cnt++;
1500 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001501
1502 smp->data.type = SMP_T_SINT;
1503 smp->data.u.sint = cnt;
1504 smp->flags = SMP_F_VOL_HDR;
1505 return 1;
1506}
1507
1508/* Fetch an HTTP header's integer value. The integer value is returned. It
1509 * takes a mandatory argument of type string and an optional one of type int
1510 * to designate a specific occurrence. It returns an unsigned integer, which
1511 * may or may not be appropriate for everything.
1512 */
1513static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1514{
1515 int ret = smp_fetch_hdr(args, smp, kw, private);
1516
1517 if (ret > 0) {
1518 smp->data.type = SMP_T_SINT;
1519 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1520 smp->data.u.str.data);
1521 }
1522
1523 return ret;
1524}
1525
1526/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1527 * and an optional one of type int to designate a specific occurrence.
1528 * It returns an IPv4 or IPv6 address.
1529 */
1530static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1531{
1532 int ret;
1533
1534 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
1535 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
1536 smp->data.type = SMP_T_IPV4;
1537 break;
1538 } else {
1539 struct buffer *temp = get_trash_chunk();
1540 if (smp->data.u.str.data < temp->size - 1) {
1541 memcpy(temp->area, smp->data.u.str.area,
1542 smp->data.u.str.data);
1543 temp->area[smp->data.u.str.data] = '\0';
1544 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1545 smp->data.type = SMP_T_IPV6;
1546 break;
1547 }
1548 }
1549 }
1550
1551 /* if the header doesn't match an IP address, fetch next one */
1552 if (!(smp->flags & SMP_F_NOT_LAST))
1553 return 0;
1554 }
1555 return ret;
1556}
1557
1558/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1559 * the first '/' after the possible hostname, and ends before the possible '?'.
1560 */
1561static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1562{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001563 struct channel *chn = SMP_REQ_CHN(smp);
1564
Christopher Faulet46575cd2019-04-17 11:40:30 +02001565 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001566 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001567 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001568 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001569 struct ist path;
1570 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001571
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001572 if (!htx)
1573 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001574
Christopher Faulet297fbb42019-05-13 14:41:27 +02001575 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001576 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001577 if (!path.ptr)
1578 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001579
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001580 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001581 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001582
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001583 /* OK, we got the '/' ! */
1584 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001585 smp->data.u.str.area = path.ptr;
1586 smp->data.u.str.data = len;
1587 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1588 }
1589 else {
1590 struct http_txn *txn;
1591 char *ptr, *end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001592
Christopher Faulet89dc4992019-04-17 12:02:59 +02001593 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001594
1595 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001596 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001597 ptr = http_txn_get_path(txn);
1598 if (!ptr)
1599 return 0;
1600
1601 /* OK, we got the '/' ! */
1602 smp->data.type = SMP_T_STR;
1603 smp->data.u.str.area = ptr;
1604
1605 while (ptr < end && *ptr != '?')
1606 ptr++;
1607
1608 smp->data.u.str.data = ptr - smp->data.u.str.area;
1609 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1610 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001611 return 1;
1612}
1613
1614/* This produces a concatenation of the first occurrence of the Host header
1615 * followed by the path component if it begins with a slash ('/'). This means
1616 * that '*' will not be added, resulting in exactly the first Host entry.
1617 * If no Host header is found, then the path is returned as-is. The returned
1618 * value is stored in the trash so it does not need to be marked constant.
1619 * The returned sample is of type string.
1620 */
1621static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1622{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001623 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001624 struct buffer *temp;
1625
Christopher Faulet46575cd2019-04-17 11:40:30 +02001626 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001627 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001628 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001629 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001630 struct http_hdr_ctx ctx;
1631 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001632
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001633 if (!htx)
1634 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001635
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001636 ctx.blk = NULL;
1637 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1638 return smp_fetch_path(args, smp, kw, private);
Willy Tarreau79e57332018-10-02 16:01:16 +02001639
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001640 /* OK we have the header value in ctx.value */
1641 temp = get_trash_chunk();
1642 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
1643
1644 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001645 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001646 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001647 if (path.ptr) {
1648 size_t len;
1649
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001650 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1651 ;
1652
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001653 if (len && *(path.ptr) == '/')
1654 chunk_memcat(temp, path.ptr, len);
1655 }
1656
1657 smp->data.type = SMP_T_STR;
1658 smp->data.u.str = *temp;
1659 }
1660 else {
1661 /* LEGACY version */
1662 struct http_txn *txn;
1663 char *ptr, *end, *beg;
1664 struct hdr_ctx ctx;
1665
Christopher Faulet89dc4992019-04-17 12:02:59 +02001666 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001667
1668 txn = smp->strm->txn;
1669 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001670 if (!http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx) || !ctx.vlen)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001671 return smp_fetch_path(args, smp, kw, private);
1672
1673 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1674 temp = get_trash_chunk();
1675 memcpy(temp->area, ctx.line + ctx.val, ctx.vlen);
1676 smp->data.type = SMP_T_STR;
1677 smp->data.u.str.area = temp->area;
1678 smp->data.u.str.data = ctx.vlen;
Willy Tarreau79e57332018-10-02 16:01:16 +02001679
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001680 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001681 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001682 beg = http_txn_get_path(txn);
1683 if (!beg)
1684 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001685
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001686 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
1687
1688 if (beg < ptr && *beg == '/') {
1689 memcpy(smp->data.u.str.area + smp->data.u.str.data, beg,
1690 ptr - beg);
1691 smp->data.u.str.data += ptr - beg;
1692 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001693 }
1694
1695 smp->flags = SMP_F_VOL_1ST;
1696 return 1;
1697}
1698
1699/* This produces a 32-bit hash of the concatenation of the first occurrence of
1700 * the Host header followed by the path component if it begins with a slash ('/').
1701 * This means that '*' will not be added, resulting in exactly the first Host
1702 * entry. If no Host header is found, then the path is used. The resulting value
1703 * is hashed using the path hash followed by a full avalanche hash and provides a
1704 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1705 * high-traffic sites without having to store whole paths.
1706 */
1707static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1708{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001709 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001710 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001711
Christopher Faulet46575cd2019-04-17 11:40:30 +02001712 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001713 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001714 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001715 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001716 struct http_hdr_ctx ctx;
1717 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001718
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001719 if (!htx)
1720 return 0;
1721
1722 ctx.blk = NULL;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001723 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001724 /* OK we have the header value in ctx.value */
1725 while (ctx.value.len--)
1726 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
1727 }
1728
1729 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02001730 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001731 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001732 if (path.ptr) {
1733 size_t len;
1734
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001735 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1736 ;
1737
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001738 if (len && *(path.ptr) == '/') {
1739 while (len--)
1740 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
1741 }
1742 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001743 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001744 else {
1745 /* LEGACY version */
1746 struct http_txn *txn;
1747 struct hdr_ctx ctx;
1748 char *ptr, *beg, *end;
1749 int len;
1750
Christopher Faulet89dc4992019-04-17 12:02:59 +02001751 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001752
1753 txn = smp->strm->txn;
1754 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001755 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001756 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
1757 ptr = ctx.line + ctx.val;
1758 len = ctx.vlen;
1759 while (len--)
1760 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
1761 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001762
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001763 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001764 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001765 beg = http_txn_get_path(txn);
1766 if (!beg)
1767 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02001768
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001769 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02001770
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001771 if (beg < ptr && *beg == '/') {
1772 while (beg < ptr)
1773 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
1774 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001775 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001776
Willy Tarreau79e57332018-10-02 16:01:16 +02001777 hash = full_hash(hash);
1778
1779 smp->data.type = SMP_T_SINT;
1780 smp->data.u.sint = hash;
1781 smp->flags = SMP_F_VOL_1ST;
1782 return 1;
1783}
1784
1785/* This concatenates the source address with the 32-bit hash of the Host and
1786 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1787 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1788 * on the source address length. The path hash is stored before the address so
1789 * that in environments where IPv6 is insignificant, truncating the output to
1790 * 8 bytes would still work.
1791 */
1792static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1793{
1794 struct buffer *temp;
1795 struct connection *cli_conn = objt_conn(smp->sess->origin);
1796
1797 if (!cli_conn)
1798 return 0;
1799
1800 if (!smp_fetch_base32(args, smp, kw, private))
1801 return 0;
1802
1803 temp = get_trash_chunk();
1804 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1805 temp->data += sizeof(unsigned int);
1806
1807 switch (cli_conn->addr.from.ss_family) {
1808 case AF_INET:
1809 memcpy(temp->area + temp->data,
1810 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1811 4);
1812 temp->data += 4;
1813 break;
1814 case AF_INET6:
1815 memcpy(temp->area + temp->data,
1816 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1817 16);
1818 temp->data += 16;
1819 break;
1820 default:
1821 return 0;
1822 }
1823
1824 smp->data.u.str = *temp;
1825 smp->data.type = SMP_T_BIN;
1826 return 1;
1827}
1828
1829/* Extracts the query string, which comes after the question mark '?'. If no
1830 * question mark is found, nothing is returned. Otherwise it returns a sample
1831 * of type string carrying the whole query string.
1832 */
1833static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1834{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001835 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001836 char *ptr, *end;
1837
Christopher Faulet46575cd2019-04-17 11:40:30 +02001838 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001839 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001840 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001841 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001842
1843 if (!htx)
1844 return 0;
1845
Christopher Faulet297fbb42019-05-13 14:41:27 +02001846 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01001847 ptr = HTX_SL_REQ_UPTR(sl);
1848 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001849 }
1850 else {
1851 /* LEGACY version */
1852 struct http_txn *txn;
1853
Christopher Faulet89dc4992019-04-17 12:02:59 +02001854 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02001855
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001856 txn = smp->strm->txn;
Christopher Faulet89dc4992019-04-17 12:02:59 +02001857 ptr = ci_head(chn) + txn->req.sl.rq.u;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001858 end = ptr + txn->req.sl.rq.u_l;
1859 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001860
1861 /* look up the '?' */
1862 do {
1863 if (ptr == end)
1864 return 0;
1865 } while (*ptr++ != '?');
1866
1867 smp->data.type = SMP_T_STR;
1868 smp->data.u.str.area = ptr;
1869 smp->data.u.str.data = end - ptr;
1870 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1871 return 1;
1872}
1873
1874static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1875{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001876 struct channel *chn = SMP_REQ_CHN(smp);
1877
Christopher Faulet46575cd2019-04-17 11:40:30 +02001878 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001879 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001880 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001881
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001882 if (!htx)
1883 return 0;
1884 }
1885 else {
1886 /* LEGACY version */
Willy Tarreau79e57332018-10-02 16:01:16 +02001887
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001888 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
1889 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
1890 */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001891 CHECK_HTTP_MESSAGE_FIRST_PERM(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001892 }
1893 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001894 smp->data.u.sint = 1;
1895 return 1;
1896}
1897
1898/* return a valid test if the current request is the first one on the connection */
1899static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1900{
1901 smp->data.type = SMP_T_BOOL;
1902 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1903 return 1;
1904}
1905
1906/* Accepts exactly 1 argument of type userlist */
1907static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1908{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001909 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001910
1911 if (!args || args->type != ARGT_USR)
1912 return 0;
1913
Christopher Faulet46575cd2019-04-17 11:40:30 +02001914 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001915 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001916 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001917
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001918 if (!htx)
1919 return 0;
Christopher Faulete98411b2019-07-15 13:58:29 +02001920 if (!get_http_auth(smp, htx))
1921 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001922 }
1923 else {
1924 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001925 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulete98411b2019-07-15 13:58:29 +02001926 if (!get_http_auth(smp, NULL))
1927 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001928 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001929
1930 smp->data.type = SMP_T_BOOL;
1931 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001932 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001933 return 1;
1934}
1935
1936/* Accepts exactly 1 argument of type userlist */
1937static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1938{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001939 struct channel *chn = SMP_REQ_CHN(smp);
1940
Willy Tarreau79e57332018-10-02 16:01:16 +02001941 if (!args || args->type != ARGT_USR)
1942 return 0;
1943
Christopher Faulet46575cd2019-04-17 11:40:30 +02001944 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001945 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02001946 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001947
1948 if (!htx)
1949 return 0;
Christopher Faulete98411b2019-07-15 13:58:29 +02001950 if (!get_http_auth(smp, htx))
1951 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001952 }
1953 else {
1954 /* LEGACY version */
Christopher Faulet89dc4992019-04-17 12:02:59 +02001955 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulete98411b2019-07-15 13:58:29 +02001956 if (!get_http_auth(smp, NULL))
1957 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001958 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001959
Willy Tarreau79e57332018-10-02 16:01:16 +02001960 /* if the user does not belong to the userlist or has a wrong password,
1961 * report that it unconditionally does not match. Otherwise we return
1962 * a string containing the username.
1963 */
1964 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1965 smp->strm->txn->auth.pass))
1966 return 0;
1967
1968 /* pat_match_auth() will need the user list */
1969 smp->ctx.a[0] = args->data.usr;
1970
1971 smp->data.type = SMP_T_STR;
1972 smp->flags = SMP_F_CONST;
1973 smp->data.u.str.area = smp->strm->txn->auth.user;
1974 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1975
1976 return 1;
1977}
1978
1979/* Fetch a captured HTTP request header. The index is the position of
1980 * the "capture" option in the configuration file
1981 */
1982static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1983{
1984 struct proxy *fe = strm_fe(smp->strm);
1985 int idx;
1986
1987 if (!args || args->type != ARGT_SINT)
1988 return 0;
1989
1990 idx = args->data.sint;
1991
1992 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1993 return 0;
1994
1995 smp->data.type = SMP_T_STR;
1996 smp->flags |= SMP_F_CONST;
1997 smp->data.u.str.area = smp->strm->req_cap[idx];
1998 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1999
2000 return 1;
2001}
2002
2003/* Fetch a captured HTTP response header. The index is the position of
2004 * the "capture" option in the configuration file
2005 */
2006static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
2007{
2008 struct proxy *fe = strm_fe(smp->strm);
2009 int idx;
2010
2011 if (!args || args->type != ARGT_SINT)
2012 return 0;
2013
2014 idx = args->data.sint;
2015
2016 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
2017 return 0;
2018
2019 smp->data.type = SMP_T_STR;
2020 smp->flags |= SMP_F_CONST;
2021 smp->data.u.str.area = smp->strm->res_cap[idx];
2022 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
2023
2024 return 1;
2025}
2026
2027/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
2028static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
2029{
2030 struct buffer *temp;
2031 struct http_txn *txn = smp->strm->txn;
2032 char *ptr;
2033
2034 if (!txn || !txn->uri)
2035 return 0;
2036
2037 ptr = txn->uri;
2038
2039 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2040 ptr++;
2041
2042 temp = get_trash_chunk();
2043 temp->area = txn->uri;
2044 temp->data = ptr - txn->uri;
2045 smp->data.u.str = *temp;
2046 smp->data.type = SMP_T_STR;
2047 smp->flags = SMP_F_CONST;
2048
2049 return 1;
2050
2051}
2052
2053/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
2054static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
2055{
2056 struct http_txn *txn = smp->strm->txn;
2057 struct ist path;
2058 const char *ptr;
2059
2060 if (!txn || !txn->uri)
2061 return 0;
2062
2063 ptr = txn->uri;
2064
2065 while (*ptr != ' ' && *ptr != '\0') /* find first space */
2066 ptr++;
2067
2068 if (!*ptr)
2069 return 0;
2070
Christopher Faulet78337bb2018-11-15 14:35:18 +01002071 /* skip the first space and find space after URI */
2072 path = ist2(++ptr, 0);
2073 while (*ptr != ' ' && *ptr != '\0')
2074 ptr++;
2075 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002076
Christopher Faulet78337bb2018-11-15 14:35:18 +01002077 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02002078 if (!path.ptr)
2079 return 0;
2080
2081 smp->data.u.str.area = path.ptr;
2082 smp->data.u.str.data = path.len;
2083 smp->data.type = SMP_T_STR;
2084 smp->flags = SMP_F_CONST;
2085
2086 return 1;
2087}
2088
2089/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
2090 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2091 */
2092static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2093{
2094 struct http_txn *txn = smp->strm->txn;
2095
2096 if (!txn || txn->req.msg_state < HTTP_MSG_HDR_FIRST)
2097 return 0;
2098
2099 if (txn->req.flags & HTTP_MSGF_VER_11)
2100 smp->data.u.str.area = "HTTP/1.1";
2101 else
2102 smp->data.u.str.area = "HTTP/1.0";
2103
2104 smp->data.u.str.data = 8;
2105 smp->data.type = SMP_T_STR;
2106 smp->flags = SMP_F_CONST;
2107 return 1;
2108
2109}
2110
2111/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
2112 * as a string (either "HTTP/1.0" or "HTTP/1.1").
2113 */
2114static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
2115{
2116 struct http_txn *txn = smp->strm->txn;
2117
2118 if (!txn || txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
2119 return 0;
2120
2121 if (txn->rsp.flags & HTTP_MSGF_VER_11)
2122 smp->data.u.str.area = "HTTP/1.1";
2123 else
2124 smp->data.u.str.area = "HTTP/1.0";
2125
2126 smp->data.u.str.data = 8;
2127 smp->data.type = SMP_T_STR;
2128 smp->flags = SMP_F_CONST;
2129 return 1;
2130
2131}
2132
2133/* Iterate over all cookies present in a message. The context is stored in
2134 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
2135 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
2136 * the direction, multiple cookies may be parsed on the same line or not.
2137 * The cookie name is in args and the name length in args->data.str.len.
2138 * Accepts exactly 1 argument of type string. If the input options indicate
2139 * that no iterating is desired, then only last value is fetched if any.
2140 * The returned sample is of type CSTR. Can be used to parse cookies in other
2141 * files.
2142 */
2143static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2144{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002145 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
2146 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002147 int occ = 0;
2148 int found = 0;
2149
2150 if (!args || args->type != ARGT_STR)
2151 return 0;
2152
Christopher Faulet46575cd2019-04-17 11:40:30 +02002153 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002154 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002155 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002156 struct http_hdr_ctx *ctx = smp->ctx.a[2];
2157 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02002158
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002159 if (!ctx) {
2160 /* first call */
2161 ctx = &static_http_hdr_ctx;
2162 ctx->blk = NULL;
2163 smp->ctx.a[2] = ctx;
2164 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002165
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002166 if (!htx)
2167 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002168
Christopher Faulet89dc4992019-04-17 12:02:59 +02002169 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002170
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002171 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2172 /* no explicit occurrence and single fetch => last cookie by default */
2173 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02002174
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002175 /* OK so basically here, either we want only one value and it's the
2176 * last one, or we want to iterate over all of them and we fetch the
2177 * next one.
Willy Tarreau79e57332018-10-02 16:01:16 +02002178 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002179
2180 if (!(smp->flags & SMP_F_NOT_LAST)) {
2181 /* search for the header from the beginning, we must first initialize
2182 * the search parameters.
2183 */
2184 smp->ctx.a[0] = NULL;
2185 ctx->blk = NULL;
2186 }
2187
2188 smp->flags |= SMP_F_VOL_HDR;
2189 while (1) {
2190 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2191 if (!smp->ctx.a[0]) {
2192 if (!http_find_header(htx, hdr, ctx, 0))
2193 goto out;
2194
2195 if (ctx->value.len < args->data.str.data + 1)
2196 continue;
2197
2198 smp->ctx.a[0] = ctx->value.ptr;
2199 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
2200 }
2201
2202 smp->data.type = SMP_T_STR;
2203 smp->flags |= SMP_F_CONST;
2204 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2205 args->data.str.area, args->data.str.data,
2206 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2207 &smp->data.u.str.area,
2208 &smp->data.u.str.data);
2209 if (smp->ctx.a[0]) {
2210 found = 1;
2211 if (occ >= 0) {
2212 /* one value was returned into smp->data.u.str.{str,len} */
2213 smp->flags |= SMP_F_NOT_LAST;
2214 return 1;
2215 }
2216 }
2217 /* if we're looking for last occurrence, let's loop */
2218 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002219 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002220 else {
2221 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002222 struct hdr_idx *idx;
2223 struct hdr_ctx *ctx = smp->ctx.a[2];
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002224 const char *hdr_name;
2225 int hdr_name_len;
2226 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002227
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002228 if (!ctx) {
2229 /* first call */
2230 ctx = &static_hdr_ctx;
2231 ctx->idx = 0;
2232 smp->ctx.a[2] = ctx;
2233 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002234
Christopher Faulet89dc4992019-04-17 12:02:59 +02002235 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002236
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002237 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002238 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002239 hdr_name = "Cookie";
2240 hdr_name_len = 6;
2241 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002242 hdr_name = "Set-Cookie";
2243 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002244 }
2245
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002246 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
2247 /* no explicit occurrence and single fetch => last cookie by default */
2248 occ = -1;
2249
2250 /* OK so basically here, either we want only one value and it's the
2251 * last one, or we want to iterate over all of them and we fetch the
2252 * next one.
2253 */
2254
Christopher Faulet89dc4992019-04-17 12:02:59 +02002255 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002256 if (!(smp->flags & SMP_F_NOT_LAST)) {
2257 /* search for the header from the beginning, we must first initialize
2258 * the search parameters.
2259 */
2260 smp->ctx.a[0] = NULL;
2261 ctx->idx = 0;
2262 }
2263
2264 smp->flags |= SMP_F_VOL_HDR;
2265
2266 while (1) {
2267 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
2268 if (!smp->ctx.a[0]) {
2269 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
2270 goto out;
2271
2272 if (ctx->vlen < args->data.str.data + 1)
2273 continue;
2274
2275 smp->ctx.a[0] = ctx->line + ctx->val;
2276 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
2277 }
2278
2279 smp->data.type = SMP_T_STR;
2280 smp->flags |= SMP_F_CONST;
2281 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
2282 args->data.str.area, args->data.str.data,
2283 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2284 &smp->data.u.str.area, &smp->data.u.str.data);
2285 if (smp->ctx.a[0]) {
2286 found = 1;
2287 if (occ >= 0) {
2288 /* one value was returned into smp->data.u.str.{str,len} */
2289 smp->flags |= SMP_F_NOT_LAST;
2290 return 1;
2291 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002292 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002293 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02002294 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002295 }
2296 /* all cookie headers and values were scanned. If we're looking for the
2297 * last occurrence, we may return it now.
2298 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002299 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02002300 smp->flags &= ~SMP_F_NOT_LAST;
2301 return found;
2302}
2303
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002304/* Same than smp_fetch_cookie() but only relies on the sample direction to
2305 * choose the right channel. So instead of duplicating the code, we just change
2306 * the keyword and then fallback on smp_fetch_cookie().
2307 */
2308static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
2309{
2310 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
2311 return smp_fetch_cookie(args, smp, kw, private);
2312}
2313
Willy Tarreau79e57332018-10-02 16:01:16 +02002314/* Iterate over all cookies present in a request to count how many occurrences
2315 * match the name in args and args->data.str.len. If <multi> is non-null, then
2316 * multiple cookies may be parsed on the same line. The returned sample is of
2317 * type UINT. Accepts exactly 1 argument of type string.
2318 */
2319static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
2320{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002321 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
2322 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Willy Tarreau79e57332018-10-02 16:01:16 +02002323 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002324 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02002325
2326 if (!args || args->type != ARGT_STR)
2327 return 0;
2328
Christopher Faulet46575cd2019-04-17 11:40:30 +02002329 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002330 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002331 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002332 struct http_hdr_ctx ctx;
2333 struct ist hdr;
2334
2335 if (!htx)
2336 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002337
Christopher Faulet89dc4992019-04-17 12:02:59 +02002338 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02002339
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002340 val_end = val_beg = NULL;
2341 ctx.blk = NULL;
2342 cnt = 0;
2343 while (1) {
2344 /* Note: val_beg == NULL every time we need to fetch a new header */
2345 if (!val_beg) {
2346 if (!http_find_header(htx, hdr, &ctx, 0))
2347 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02002348
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002349 if (ctx.value.len < args->data.str.data + 1)
2350 continue;
Willy Tarreau79e57332018-10-02 16:01:16 +02002351
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002352 val_beg = ctx.value.ptr;
2353 val_end = val_beg + ctx.value.len;
2354 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002355
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002356 smp->data.type = SMP_T_STR;
2357 smp->flags |= SMP_F_CONST;
2358 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2359 args->data.str.area, args->data.str.data,
2360 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2361 &smp->data.u.str.area,
2362 &smp->data.u.str.data))) {
2363 cnt++;
2364 }
2365 }
2366 }
2367 else {
2368 /* LEGACY version */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002369 struct hdr_idx *idx;
2370 struct hdr_ctx ctx;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002371 const char *hdr_name;
2372 int hdr_name_len;
2373 char *sol;
Willy Tarreau79e57332018-10-02 16:01:16 +02002374
Christopher Faulet89dc4992019-04-17 12:02:59 +02002375 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002376
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002377 idx = &smp->strm->txn->hdr_idx;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002378 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002379 hdr_name = "Cookie";
2380 hdr_name_len = 6;
2381 } else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002382 hdr_name = "Set-Cookie";
2383 hdr_name_len = 10;
Willy Tarreau79e57332018-10-02 16:01:16 +02002384 }
2385
Christopher Faulet89dc4992019-04-17 12:02:59 +02002386 sol = ci_head(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002387 val_end = val_beg = NULL;
2388 ctx.idx = 0;
2389 cnt = 0;
2390
2391 while (1) {
2392 /* Note: val_beg == NULL every time we need to fetch a new header */
2393 if (!val_beg) {
2394 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
2395 break;
2396
2397 if (ctx.vlen < args->data.str.data + 1)
2398 continue;
2399
2400 val_beg = ctx.line + ctx.val;
2401 val_end = val_beg + ctx.vlen;
2402 }
2403
2404 smp->data.type = SMP_T_STR;
2405 smp->flags |= SMP_F_CONST;
2406 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
2407 args->data.str.area, args->data.str.data,
2408 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
2409 &smp->data.u.str.area, &smp->data.u.str.data))) {
2410 cnt++;
2411 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002412 }
2413 }
2414
2415 smp->data.type = SMP_T_SINT;
2416 smp->data.u.sint = cnt;
2417 smp->flags |= SMP_F_VOL_HDR;
2418 return 1;
2419}
2420
2421/* Fetch an cookie's integer value. The integer value is returned. It
2422 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
2423 */
2424static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2425{
2426 int ret = smp_fetch_cookie(args, smp, kw, private);
2427
2428 if (ret > 0) {
2429 smp->data.type = SMP_T_SINT;
2430 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2431 smp->data.u.str.data);
2432 }
2433
2434 return ret;
2435}
2436
2437/************************************************************************/
2438/* The code below is dedicated to sample fetches */
2439/************************************************************************/
2440
2441/* This scans a URL-encoded query string. It takes an optionally wrapping
2442 * string whose first contigous chunk has its beginning in ctx->a[0] and end
2443 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
2444 * pointers are updated for next iteration before leaving.
2445 */
2446static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
2447{
2448 const char *vstart, *vend;
2449 struct buffer *temp;
2450 const char **chunks = (const char **)smp->ctx.a;
2451
2452 if (!http_find_next_url_param(chunks, name, name_len,
2453 &vstart, &vend, delim))
2454 return 0;
2455
2456 /* Create sample. If the value is contiguous, return the pointer as CONST,
2457 * if the value is wrapped, copy-it in a buffer.
2458 */
2459 smp->data.type = SMP_T_STR;
2460 if (chunks[2] &&
2461 vstart >= chunks[0] && vstart <= chunks[1] &&
2462 vend >= chunks[2] && vend <= chunks[3]) {
2463 /* Wrapped case. */
2464 temp = get_trash_chunk();
2465 memcpy(temp->area, vstart, chunks[1] - vstart);
2466 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
2467 vend - chunks[2]);
2468 smp->data.u.str.area = temp->area;
2469 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
2470 } else {
2471 /* Contiguous case. */
2472 smp->data.u.str.area = (char *)vstart;
2473 smp->data.u.str.data = vend - vstart;
2474 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
2475 }
2476
2477 /* Update context, check wrapping. */
2478 chunks[0] = vend;
2479 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
2480 chunks[1] = chunks[3];
2481 chunks[2] = NULL;
2482 }
2483
2484 if (chunks[0] < chunks[1])
2485 smp->flags |= SMP_F_NOT_LAST;
2486
2487 return 1;
2488}
2489
2490/* This function iterates over each parameter of the query string. It uses
2491 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
2492 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
2493 * An optional parameter name is passed in args[0], otherwise any parameter is
2494 * considered. It supports an optional delimiter argument for the beginning of
2495 * the string in args[1], which defaults to "?".
2496 */
2497static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2498{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002499 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002500 char delim = '?';
2501 const char *name;
2502 int name_len;
2503
2504 if (!args ||
2505 (args[0].type && args[0].type != ARGT_STR) ||
2506 (args[1].type && args[1].type != ARGT_STR))
2507 return 0;
2508
2509 name = "";
2510 name_len = 0;
2511 if (args->type == ARGT_STR) {
2512 name = args->data.str.area;
2513 name_len = args->data.str.data;
2514 }
2515
2516 if (args[1].type)
2517 delim = *args[1].data.str.area;
2518
2519 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002520 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002521 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002522 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002523 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02002524
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002525 if (!htx)
2526 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002527
Christopher Faulet297fbb42019-05-13 14:41:27 +02002528 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002529 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 +02002530 if (!smp->ctx.a[0])
2531 return 0;
2532
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002533 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002534 }
2535 else {
2536 /* LEGACY version */
2537 struct http_msg *msg;
2538
Christopher Faulet89dc4992019-04-17 12:02:59 +02002539 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002540
2541 msg = &smp->strm->txn->req;
2542
Christopher Faulet89dc4992019-04-17 12:02:59 +02002543 smp->ctx.a[0] = http_find_param_list(ci_head(chn) + msg->sl.rq.u,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002544 msg->sl.rq.u_l, delim);
2545 if (!smp->ctx.a[0])
2546 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002547
Christopher Faulet89dc4992019-04-17 12:02:59 +02002548 smp->ctx.a[1] = ci_head(chn) + msg->sl.rq.u + msg->sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002549 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002550
2551 /* Assume that the context is filled with NULL pointer
2552 * before the first call.
2553 * smp->ctx.a[2] = NULL;
2554 * smp->ctx.a[3] = NULL;
2555 */
2556 }
2557
2558 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
2559}
2560
2561/* This function iterates over each parameter of the body. This requires
2562 * that the body has been waited for using http-buffer-request. It uses
2563 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
2564 * contigous part of the body, and optionally ctx->a[2..3] to reference the
2565 * optional second part if the body wraps at the end of the buffer. An optional
2566 * parameter name is passed in args[0], otherwise any parameter is considered.
2567 */
2568static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
2569{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002570 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002571 const char *name;
2572 int name_len;
2573
2574 if (!args || (args[0].type && args[0].type != ARGT_STR))
2575 return 0;
2576
2577 name = "";
2578 name_len = 0;
2579 if (args[0].type == ARGT_STR) {
2580 name = args[0].data.str.area;
2581 name_len = args[0].data.str.data;
2582 }
2583
2584 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet46575cd2019-04-17 11:40:30 +02002585 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002586 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002587 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002588 struct buffer *temp;
2589 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02002590
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002591 if (!htx)
2592 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002593
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002594 temp = get_trash_chunk();
Christopher Fauleta3f15502019-05-13 15:27:23 +02002595 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002596 struct htx_blk *blk = htx_get_blk(htx, pos);
2597 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02002598
Christopher Faulet54b5e212019-06-04 10:08:28 +02002599 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002600 break;
2601 if (type == HTX_BLK_DATA) {
Christopher Fauletc59ff232018-12-03 13:58:44 +01002602 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002603 return 0;
2604 }
2605 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002606
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002607 smp->ctx.a[0] = temp->area;
2608 smp->ctx.a[1] = temp->area + temp->data;
Willy Tarreau79e57332018-10-02 16:01:16 +02002609
2610 /* Assume that the context is filled with NULL pointer
2611 * before the first call.
2612 * smp->ctx.a[2] = NULL;
2613 * smp->ctx.a[3] = NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002614 */
Willy Tarreau79e57332018-10-02 16:01:16 +02002615 }
2616 else {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002617 /* LEGACY version */
2618 struct http_msg *msg;
2619 unsigned long len;
2620 unsigned long block1;
2621 char *body;
2622
Christopher Faulet89dc4992019-04-17 12:02:59 +02002623 CHECK_HTTP_MESSAGE_FIRST(chn);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002624
Christopher Faulet89dc4992019-04-17 12:02:59 +02002625 msg = &smp->strm->txn->req;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002626 len = http_body_bytes(msg);
Christopher Faulet89dc4992019-04-17 12:02:59 +02002627 body = c_ptr(chn, -http_data_rewind(msg));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002628
2629 block1 = len;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002630 if (block1 > b_wrap(&chn->buf) - body)
2631 block1 = b_wrap(&chn->buf) - body;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002632
2633 if (block1 == len) {
2634 /* buffer is not wrapped (or empty) */
2635 smp->ctx.a[0] = body;
2636 smp->ctx.a[1] = body + len;
2637
2638 /* Assume that the context is filled with NULL pointer
2639 * before the first call.
2640 * smp->ctx.a[2] = NULL;
2641 * smp->ctx.a[3] = NULL;
2642 */
2643 }
2644 else {
2645 /* buffer is wrapped, we need to defragment it */
2646 smp->ctx.a[0] = body;
2647 smp->ctx.a[1] = body + block1;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002648 smp->ctx.a[2] = b_orig(&chn->buf);
2649 smp->ctx.a[3] = b_orig(&chn->buf) + ( len - block1 );
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002650 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002651 }
2652 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002653
Willy Tarreau79e57332018-10-02 16:01:16 +02002654 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
2655}
2656
2657/* Return the signed integer value for the specified url parameter (see url_param
2658 * above).
2659 */
2660static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2661{
2662 int ret = smp_fetch_url_param(args, smp, kw, private);
2663
2664 if (ret > 0) {
2665 smp->data.type = SMP_T_SINT;
2666 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2667 smp->data.u.str.data);
2668 }
2669
2670 return ret;
2671}
2672
2673/* This produces a 32-bit hash of the concatenation of the first occurrence of
2674 * the Host header followed by the path component if it begins with a slash ('/').
2675 * This means that '*' will not be added, resulting in exactly the first Host
2676 * entry. If no Host header is found, then the path is used. The resulting value
2677 * is hashed using the url hash followed by a full avalanche hash and provides a
2678 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2679 * high-traffic sites without having to store whole paths.
2680 * this differs from the base32 functions in that it includes the url parameters
2681 * as well as the path
2682 */
2683static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2684{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002685 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02002686 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02002687
Christopher Faulet46575cd2019-04-17 11:40:30 +02002688 if (smp->px->options2 & PR_O2_USE_HTX) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002689 /* HTX version */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +02002690 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002691 struct http_hdr_ctx ctx;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002692 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002693 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002694
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002695 if (!htx)
2696 return 0;
2697
2698 ctx.blk = NULL;
2699 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2700 /* OK we have the header value in ctx.value */
2701 while (ctx.value.len--)
2702 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
2703 }
2704
2705 /* now retrieve the path */
Christopher Faulet297fbb42019-05-13 14:41:27 +02002706 sl = http_get_stline(htx);
Christopher Fauletf1ba18d2018-11-26 21:37:08 +01002707 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002708 while (path.len > 0 && *(path.ptr) != '?') {
2709 path.ptr++;
2710 path.len--;
2711 }
2712 if (path.len && *(path.ptr) == '/') {
2713 while (path.len--)
2714 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
2715 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002716 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002717 else {
2718 /* LEGACY version */
2719 struct http_txn *txn;
2720 struct hdr_ctx ctx;
2721 char *ptr, *beg, *end;
2722 int len;
2723
Christopher Faulet89dc4992019-04-17 12:02:59 +02002724 CHECK_HTTP_MESSAGE_FIRST(chn);
Willy Tarreau79e57332018-10-02 16:01:16 +02002725
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002726 txn = smp->strm->txn;
2727 ctx.idx = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +02002728 if (http_find_header2("Host", 4, ci_head(chn), &txn->hdr_idx, &ctx)) {
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002729 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
2730 ptr = ctx.line + ctx.val;
2731 len = ctx.vlen;
2732 while (len--)
2733 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
2734 }
2735
2736 /* now retrieve the path */
Christopher Faulet89dc4992019-04-17 12:02:59 +02002737 end = ci_head(chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002738 beg = http_txn_get_path(txn);
2739 if (!beg)
2740 beg = end;
Willy Tarreau79e57332018-10-02 16:01:16 +02002741
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002742 for (ptr = beg; ptr < end ; ptr++);
Willy Tarreau79e57332018-10-02 16:01:16 +02002743
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002744 if (beg < ptr && *beg == '/') {
2745 while (beg < ptr)
2746 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
2747 }
Willy Tarreau79e57332018-10-02 16:01:16 +02002748 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002749
Willy Tarreau79e57332018-10-02 16:01:16 +02002750 hash = full_hash(hash);
2751
2752 smp->data.type = SMP_T_SINT;
2753 smp->data.u.sint = hash;
2754 smp->flags = SMP_F_VOL_1ST;
2755 return 1;
2756}
2757
2758/* This concatenates the source address with the 32-bit hash of the Host and
2759 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2760 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2761 * on the source address length. The URL hash is stored before the address so
2762 * that in environments where IPv6 is insignificant, truncating the output to
2763 * 8 bytes would still work.
2764 */
2765static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2766{
2767 struct buffer *temp;
2768 struct connection *cli_conn = objt_conn(smp->sess->origin);
2769
2770 if (!cli_conn)
2771 return 0;
2772
2773 if (!smp_fetch_url32(args, smp, kw, private))
2774 return 0;
2775
2776 temp = get_trash_chunk();
2777 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2778 temp->data += sizeof(unsigned int);
2779
2780 switch (cli_conn->addr.from.ss_family) {
2781 case AF_INET:
2782 memcpy(temp->area + temp->data,
2783 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
2784 4);
2785 temp->data += 4;
2786 break;
2787 case AF_INET6:
2788 memcpy(temp->area + temp->data,
2789 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
2790 16);
2791 temp->data += 16;
2792 break;
2793 default:
2794 return 0;
2795 }
2796
2797 smp->data.u.str = *temp;
2798 smp->data.type = SMP_T_BIN;
2799 return 1;
2800}
2801
2802/************************************************************************/
2803/* Other utility functions */
2804/************************************************************************/
2805
2806/* This function is used to validate the arguments passed to any "hdr" fetch
2807 * keyword. These keywords support an optional positive or negative occurrence
2808 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2809 * is assumed that the types are already the correct ones. Returns 0 on error,
2810 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2811 * error message in case of error, that the caller is responsible for freeing.
2812 * The initial location must either be freeable or NULL.
2813 * Note: this function's pointer is checked from Lua.
2814 */
2815int val_hdr(struct arg *arg, char **err_msg)
2816{
2817 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2818 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2819 return 0;
2820 }
2821 return 1;
2822}
2823
2824/************************************************************************/
2825/* All supported sample fetch keywords must be declared here. */
2826/************************************************************************/
2827
2828/* Note: must not be declared <const> as its list will be overwritten */
2829static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2830 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2831 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2832 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2833
2834 /* capture are allocated and are permanent in the stream */
2835 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2836
2837 /* retrieve these captures from the HTTP logs */
2838 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2839 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2840 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2841
2842 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2843 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2844
2845 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2846 * are only here to match the ACL's name, are request-only and are used
2847 * for ACL compatibility only.
2848 */
2849 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002850 { "cookie", smp_fetch_chn_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002851 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2852 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2853
2854 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2855 * only here to match the ACL's name, are request-only and are used for
2856 * ACL compatibility only.
2857 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002858 { "hdr", smp_fetch_chn_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002859 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2860 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2861 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2862
2863 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2864 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2865 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2866 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2867 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2868 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2869
2870 /* HTTP protocol on the request path */
2871 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2872 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2873
2874 /* HTTP version on the request path */
2875 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2876 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2877
2878 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2879 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2880 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2881 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2882
2883 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2884 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2885
2886 /* HTTP version on the response path */
2887 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2888 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2889
2890 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2891 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2892 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2893 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2894
2895 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2896 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2897 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2898 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2899 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2900 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2901 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2902
2903 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2904 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2905 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2906 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2907
2908 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2909 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2910 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2911 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2912 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2913 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2914 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2915
2916 /* scook is valid only on the response and is used for ACL compatibility */
2917 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2918 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2919 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2920 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2921
2922 /* shdr is valid only on the response and is used for ACL compatibility */
2923 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2924 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2925 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2926 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2927
2928 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2929 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2930 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2931 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2932 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2933 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2934 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2935 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2936 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2937 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2938 { /* END */ },
2939}};
2940
Willy Tarreau0108d902018-11-25 19:14:37 +01002941INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002942
2943/*
2944 * Local variables:
2945 * c-indent-level: 8
2946 * c-basic-offset: 8
2947 * End:
2948 */