blob: bf4c00680920e1fa48bb915c9ba8880d88f9c617 [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>
Christopher Fauleteb2754b2019-07-16 14:49:01 +020036#include <proto/channel.h>
Willy Tarreau9a1efe12019-07-17 17:13:50 +020037#include <proto/connection.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020038#include <proto/http_fetch.h>
Christopher Faulet53a899b2019-10-08 16:38:42 +020039#include <proto/h1_htx.h>
Christopher Fauletef453ed2018-10-24 21:39:27 +020040#include <proto/http_htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020041#include <proto/log.h>
42#include <proto/obj_type.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020043#include <proto/http_ana.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020044#include <proto/sample.h>
45#include <proto/stream.h>
46
47
48/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Fauletef453ed2018-10-24 21:39:27 +020049static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
Richard Russo458eafb2019-07-31 11:45:56 -070050/* this is used to convert raw connection buffers to htx */
51static THREAD_LOCAL struct buffer static_raw_htx_chunk;
52static THREAD_LOCAL char *static_raw_htx_buf;
Christopher Fauletef453ed2018-10-24 21:39:27 +020053
Christopher Faulet89dc4992019-04-17 12:02:59 +020054#define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL)
55#define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL)
Willy Tarreau79e57332018-10-02 16:01:16 +020056
Richard Russo458eafb2019-07-31 11:45:56 -070057/* This function returns the static htx chunk, where raw connections get
58 * converted to HTX as needed for samplxsing.
59 */
60struct buffer *get_raw_htx_chunk(void)
61{
62 chunk_reset(&static_raw_htx_chunk);
63 return &static_raw_htx_chunk;
64}
65
66static int alloc_raw_htx_chunk_per_thread()
67{
68 static_raw_htx_buf = malloc(global.tune.bufsize);
69 if (!static_raw_htx_buf)
70 return 0;
71 chunk_init(&static_raw_htx_chunk, static_raw_htx_buf, global.tune.bufsize);
72 return 1;
73}
74
75static void free_raw_htx_chunk_per_thread()
76{
77 free(static_raw_htx_buf);
78 static_raw_htx_buf = NULL;
79}
80
81REGISTER_PER_THREAD_ALLOC(alloc_raw_htx_chunk_per_thread);
82REGISTER_PER_THREAD_FREE(free_raw_htx_chunk_per_thread);
83
Willy Tarreau79e57332018-10-02 16:01:16 +020084/*
85 * Returns the data from Authorization header. Function may be called more
86 * than once so data is stored in txn->auth_data. When no header is found
87 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
88 * searching again for something we are unable to find anyway. However, if
89 * the result if valid, the cache is not reused because we would risk to
90 * have the credentials overwritten by another stream in parallel.
91 */
92
Christopher Fauletcd761952019-07-15 13:58:29 +020093static int get_http_auth(struct sample *smp, struct htx *htx)
Willy Tarreau79e57332018-10-02 16:01:16 +020094{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020095 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020096 struct http_txn *txn = s->txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020097 struct http_hdr_ctx ctx = { .blk = NULL };
98 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020099 struct buffer auth_method;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200100 char *p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200101 int len;
102
103#ifdef DEBUG_AUTH
104 printf("Auth for stream %p: %d\n", s, txn->auth.method);
105#endif
Willy Tarreau79e57332018-10-02 16:01:16 +0200106 if (txn->auth.method == HTTP_AUTH_WRONG)
107 return 0;
108
109 txn->auth.method = HTTP_AUTH_WRONG;
110
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200111 if (txn->flags & TX_USE_PX_CONN)
112 hdr = ist("Proxy-Authorization");
113 else
114 hdr = ist("Authorization");
Willy Tarreau79e57332018-10-02 16:01:16 +0200115
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200116 ctx.blk = NULL;
117 if (!http_find_header(htx, hdr, &ctx, 0))
118 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200119
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200120 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
121 len = p - ctx.value.ptr;
122 if (!p || len <= 0)
123 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200124
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200125 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
126 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200127
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200128 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
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
Richard Russo458eafb2019-07-31 11:45:56 -0700268 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200269 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
Willy Tarreau79e57332018-10-02 16:01:16 +0200296/* This function fetches the method of current HTTP request and stores
297 * it in the global pattern struct as a chunk. There are two possibilities :
298 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
299 * in <len> and <ptr> is NULL ;
300 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
301 * <len> to its length.
302 * This is intended to be used with pat_match_meth() only.
303 */
304static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
305{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200306 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200307 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +0200308 struct http_txn *txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200309 int meth;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200310
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200311 if (!htx)
312 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200313
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200314 txn = smp->strm->txn;
315 meth = txn->meth;
316 smp->data.type = SMP_T_METH;
317 smp->data.u.meth.meth = meth;
318 if (meth == HTTP_METH_OTHER) {
319 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200320
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200321 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
322 /* ensure the indexes are not affected */
323 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200324 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200325 sl = http_get_stline(htx);
326 smp->flags |= SMP_F_CONST;
327 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
328 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200329 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200330 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200331 return 1;
332}
333
334static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
335{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200336 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200337 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
338 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200339 char *ptr;
340 int len;
341
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200342 if (!htx)
343 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200344
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200345 sl = http_get_stline(htx);
346 len = HTX_SL_REQ_VLEN(sl);
347 ptr = HTX_SL_REQ_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200348
349 while ((len-- > 0) && (*ptr++ != '/'));
350 if (len <= 0)
351 return 0;
352
353 smp->data.type = SMP_T_STR;
354 smp->data.u.str.area = ptr;
355 smp->data.u.str.data = len;
356
357 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
358 return 1;
359}
360
361static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
362{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200363 struct channel *chn = SMP_RES_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200364 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
365 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200366 char *ptr;
367 int len;
368
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200369 if (!htx)
370 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200371
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200372 sl = http_get_stline(htx);
373 len = HTX_SL_RES_VLEN(sl);
374 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200375
376 while ((len-- > 0) && (*ptr++ != '/'));
377 if (len <= 0)
378 return 0;
379
380 smp->data.type = SMP_T_STR;
381 smp->data.u.str.area = ptr;
382 smp->data.u.str.data = len;
383
384 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
385 return 1;
386}
387
388/* 3. Check on Status Code. We manipulate integers here. */
389static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
390{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200391 struct channel *chn = SMP_RES_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200392 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
393 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200394 char *ptr;
395 int len;
396
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200397 if (!htx)
398 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200399
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200400 sl = http_get_stline(htx);
401 len = HTX_SL_RES_CLEN(sl);
402 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200403
404 smp->data.type = SMP_T_SINT;
405 smp->data.u.sint = __strl2ui(ptr, len);
406 smp->flags = SMP_F_VOL_1ST;
407 return 1;
408}
409
410static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
411{
412 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
413 return 0;
414
415 if (!smp->strm->unique_id) {
416 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
417 return 0;
418 smp->strm->unique_id[0] = '\0';
419 }
420 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
421 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
422
423 smp->data.type = SMP_T_STR;
424 smp->data.u.str.area = smp->strm->unique_id;
425 smp->flags = SMP_F_CONST;
426 return 1;
427}
428
429/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800430 * empty line which separes headers from the body. This is useful
431 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200432 */
433static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
434{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200435 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200436 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
437 struct buffer *temp;
438 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200439
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200440 if (!htx)
441 return 0;
442 temp = get_trash_chunk();
443 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
444 struct htx_blk *blk = htx_get_blk(htx, pos);
445 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200446
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200447 if (type == HTX_BLK_HDR) {
448 struct ist n = htx_get_blk_name(htx, blk);
449 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200450
Christopher Faulet53a899b2019-10-08 16:38:42 +0200451 if (!h1_format_htx_hdr(n, v, temp))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200452 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200453 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200454 else if (type == HTX_BLK_EOH) {
455 if (!chunk_memcat(temp, "\r\n", 2))
456 return 0;
457 break;
458 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200459 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200460 smp->data.type = SMP_T_STR;
461 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200462 return 1;
463}
464
465/* Returns the header request in a length/value encoded format.
466 * This is useful for exchanges with the SPOE.
467 *
468 * A "length value" is a multibyte code encoding numbers. It uses the
469 * SPOE format. The encoding is the following:
470 *
471 * Each couple "header name" / "header value" is composed
472 * like this:
473 * "length value" "header name bytes"
474 * "length value" "header value bytes"
475 * When the last header is reached, the header name and the header
476 * value are empty. Their length are 0
477 */
478static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
479{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200480 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200481 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200482 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200483 char *p, *end;
484 int32_t pos;
485 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200486
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200487 if (!htx)
488 return 0;
489 temp = get_trash_chunk();
490 p = temp->area;
491 end = temp->area + temp->size;
492 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
493 struct htx_blk *blk = htx_get_blk(htx, pos);
494 enum htx_blk_type type = htx_get_blk_type(blk);
495 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200496
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200497 if (type == HTX_BLK_HDR) {
498 n = htx_get_blk_name(htx,blk);
499 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200500
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200501 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200502 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200503 if (ret == -1)
504 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200505 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200506 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200507 memcpy(p, n.ptr, n.len);
508 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200509
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200510 /* encode the header value. */
511 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200512 if (ret == -1)
513 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200514 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200515 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200516 memcpy(p, v.ptr, v.len);
517 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200518
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200519 }
520 else if (type == HTX_BLK_EOH) {
521 /* encode the end of the header list with empty
522 * header name and header value.
523 */
524 ret = encode_varint(0, &p, end);
525 if (ret == -1)
526 return 0;
527 ret = encode_varint(0, &p, end);
528 if (ret == -1)
529 return 0;
530 break;
531 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200532 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200533
534 /* Initialise sample data which will be filled. */
535 smp->data.type = SMP_T_BIN;
536 smp->data.u.str.area = temp->area;
537 smp->data.u.str.data = p - temp->area;
538 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200539 return 1;
540}
541
542/* returns the longest available part of the body. This requires that the body
543 * has been waited for using http-buffer-request.
544 */
545static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
546{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200547 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200548 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200549 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200550 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200551
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200552 if (!htx)
553 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200554
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200555 temp = get_trash_chunk();
556 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
557 struct htx_blk *blk = htx_get_blk(htx, pos);
558 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200559
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200560 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
561 break;
562 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +0200563 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200564 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200565 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200566 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200567
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200568 smp->data.type = SMP_T_BIN;
569 smp->data.u.str = *temp;
570 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200571 return 1;
572}
573
574
575/* returns the available length of the body. This requires that the body
576 * has been waited for using http-buffer-request.
577 */
578static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
579{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200580 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200581 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
582 int32_t pos;
583 unsigned long long len = 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100584
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200585 if (!htx)
586 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100587
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200588 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
589 struct htx_blk *blk = htx_get_blk(htx, pos);
590 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100591
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200592 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
593 break;
594 if (type == HTX_BLK_DATA)
595 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200596 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200597
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200598 smp->data.type = SMP_T_SINT;
599 smp->data.u.sint = len;
600 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200601 return 1;
602}
603
604
605/* returns the advertised length of the body, or the advertised size of the
606 * chunks available in the buffer. This requires that the body has been waited
607 * for using http-buffer-request.
608 */
609static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
610{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200611 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200612 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
613 int32_t pos;
614 unsigned long long len = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200615
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200616 if (!htx)
617 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100618
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200619 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
620 struct htx_blk *blk = htx_get_blk(htx, pos);
621 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100622
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200623 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
624 break;
625 if (type == HTX_BLK_DATA)
626 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200627 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200628 if (htx->extra != ULLONG_MAX)
629 len += htx->extra;
Willy Tarreau79e57332018-10-02 16:01:16 +0200630
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200631 smp->data.type = SMP_T_SINT;
632 smp->data.u.sint = len;
633 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200634 return 1;
635}
636
637
638/* 4. Check on URL/URI. A pointer to the URI is stored. */
639static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
640{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200641 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200642 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
643 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200644
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200645 if (!htx)
646 return 0;
647 sl = http_get_stline(htx);
648 smp->data.type = SMP_T_STR;
649 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
650 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
651 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200652 return 1;
653}
654
655static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
656{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200657 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200658 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
659 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200660 struct sockaddr_storage addr;
661
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200662 if (!htx)
663 return 0;
664 sl = http_get_stline(htx);
665 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +0200666
Willy Tarreau79e57332018-10-02 16:01:16 +0200667 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
668 return 0;
669
670 smp->data.type = SMP_T_IPV4;
671 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
672 smp->flags = 0;
673 return 1;
674}
675
676static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
677{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200678 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200679 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
680 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200681 struct sockaddr_storage addr;
682
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200683 if (!htx)
684 return 0;
685 sl = http_get_stline(htx);
686 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200687
Willy Tarreau79e57332018-10-02 16:01:16 +0200688 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
689 return 0;
690
691 smp->data.type = SMP_T_SINT;
692 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
693 smp->flags = 0;
694 return 1;
695}
696
697/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
698 * Accepts an optional argument of type string containing the header field name,
699 * and an optional argument of type signed or unsigned integer to request an
700 * explicit occurrence of the header. Note that in the event of a missing name,
701 * headers are considered from the first one. It does not stop on commas and
702 * returns full lines instead (useful for User-Agent or Date for example).
703 */
704static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
705{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200706 /* possible keywords: req.fhdr, res.fhdr */
707 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200708 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
709 struct http_hdr_ctx *ctx = smp->ctx.a[0];
710 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200711 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200712
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200713 if (!ctx) {
714 /* first call */
715 ctx = &static_http_hdr_ctx;
716 ctx->blk = NULL;
717 smp->ctx.a[0] = ctx;
718 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200719
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200720 if (args) {
721 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200722 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200723 name.ptr = args[0].data.str.area;
724 name.len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +0200725
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200726 if (args[1].type == ARGT_SINT)
727 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200728 }
729
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200730 if (!htx)
731 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200732
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200733 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
734 /* search for header from the beginning */
735 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200736
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200737 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
738 /* no explicit occurrence and single fetch => last header by default */
739 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200740
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200741 if (!occ)
742 /* prepare to report multiple occurrences for ACL fetches */
743 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200744
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200745 smp->data.type = SMP_T_STR;
746 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
747 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
748 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200749 smp->flags &= ~SMP_F_NOT_LAST;
750 return 0;
751}
752
753/* 6. Check on HTTP header count. The number of occurrences is returned.
754 * Accepts exactly 1 argument of type string. It does not stop on commas and
755 * returns full lines instead (useful for User-Agent or Date for example).
756 */
757static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
758{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200759 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
760 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200761 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
762 struct http_hdr_ctx ctx;
763 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200764 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200765
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200766 if (!htx)
767 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200768
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200769 if (args && args->type == ARGT_STR) {
770 name.ptr = args->data.str.area;
771 name.len = args->data.str.data;
772 } else {
773 name.ptr = NULL;
774 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200775 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200776
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200777 ctx.blk = NULL;
778 cnt = 0;
779 while (http_find_header(htx, name, &ctx, 1))
780 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200781 smp->data.type = SMP_T_SINT;
782 smp->data.u.sint = cnt;
783 smp->flags = SMP_F_VOL_HDR;
784 return 1;
785}
786
787static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
788{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200789 /* possible keywords: req.hdr_names, res.hdr_names */
790 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200791 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200792 struct buffer *temp;
793 char del = ',';
794
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200795 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200796
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200797 if (!htx)
798 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200799
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200800 if (args && args->type == ARGT_STR)
801 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200802
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200803 temp = get_trash_chunk();
804 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
805 struct htx_blk *blk = htx_get_blk(htx, pos);
806 enum htx_blk_type type = htx_get_blk_type(blk);
807 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200808
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200809 if (type == HTX_BLK_EOH)
810 break;
811 if (type != HTX_BLK_HDR)
812 continue;
813 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200814
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200815 if (temp->data)
816 temp->area[temp->data++] = del;
817 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200818 }
819
820 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200821 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200822 smp->flags = SMP_F_VOL_HDR;
823 return 1;
824}
825
826/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
827 * Accepts an optional argument of type string containing the header field name,
828 * and an optional argument of type signed or unsigned integer to request an
829 * explicit occurrence of the header. Note that in the event of a missing name,
830 * headers are considered from the first one.
831 */
832static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
833{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200834 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
835 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200836 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
837 struct http_hdr_ctx *ctx = smp->ctx.a[0];
838 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200839 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200840
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200841 if (!ctx) {
842 /* first call */
843 ctx = &static_http_hdr_ctx;
844 ctx->blk = NULL;
845 smp->ctx.a[0] = ctx;
846 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200847
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200848 if (args) {
849 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200850 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200851 name.ptr = args[0].data.str.area;
852 name.len = args[0].data.str.data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200853
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200854 if (args[1].type == ARGT_SINT)
855 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200856 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200857
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200858 if (!htx)
859 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200860
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200861 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
862 /* search for header from the beginning */
863 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200864
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200865 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
866 /* no explicit occurrence and single fetch => last header by default */
867 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200868
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200869 if (!occ)
870 /* prepare to report multiple occurrences for ACL fetches */
871 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200872
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200873 smp->data.type = SMP_T_STR;
874 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
875 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
876 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200877
878 smp->flags &= ~SMP_F_NOT_LAST;
879 return 0;
880}
881
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200882/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
883 * the right channel. So instead of duplicating the code, we just change the
884 * keyword and then fallback on smp_fetch_hdr().
885 */
886static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
887{
888 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
889 return smp_fetch_hdr(args, smp, kw, private);
890}
891
Willy Tarreau79e57332018-10-02 16:01:16 +0200892/* 6. Check on HTTP header count. The number of occurrences is returned.
893 * Accepts exactly 1 argument of type string.
894 */
895static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
896{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200897 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
898 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200899 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
900 struct http_hdr_ctx ctx;
901 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200902 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200903
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200904 if (!htx)
905 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200906
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200907 if (args && args->type == ARGT_STR) {
908 name.ptr = args->data.str.area;
909 name.len = args->data.str.data;
910 } else {
911 name.ptr = NULL;
912 name.len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200913 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200914
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200915 ctx.blk = NULL;
916 cnt = 0;
917 while (http_find_header(htx, name, &ctx, 0))
918 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200919
920 smp->data.type = SMP_T_SINT;
921 smp->data.u.sint = cnt;
922 smp->flags = SMP_F_VOL_HDR;
923 return 1;
924}
925
926/* Fetch an HTTP header's integer value. The integer value is returned. It
927 * takes a mandatory argument of type string and an optional one of type int
928 * to designate a specific occurrence. It returns an unsigned integer, which
929 * may or may not be appropriate for everything.
930 */
931static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
932{
933 int ret = smp_fetch_hdr(args, smp, kw, private);
934
935 if (ret > 0) {
936 smp->data.type = SMP_T_SINT;
937 smp->data.u.sint = strl2ic(smp->data.u.str.area,
938 smp->data.u.str.data);
939 }
940
941 return ret;
942}
943
944/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
945 * and an optional one of type int to designate a specific occurrence.
946 * It returns an IPv4 or IPv6 address.
947 */
948static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
949{
950 int ret;
951
952 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
953 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
954 smp->data.type = SMP_T_IPV4;
955 break;
956 } else {
957 struct buffer *temp = get_trash_chunk();
958 if (smp->data.u.str.data < temp->size - 1) {
959 memcpy(temp->area, smp->data.u.str.area,
960 smp->data.u.str.data);
961 temp->area[smp->data.u.str.data] = '\0';
962 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
963 smp->data.type = SMP_T_IPV6;
964 break;
965 }
966 }
967 }
968
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200969 /* if the header doesn't match an IP address, fetch next one */
970 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200971 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200972 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200973 return ret;
974}
Willy Tarreau79e57332018-10-02 16:01:16 +0200975
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200976/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
977 * the first '/' after the possible hostname, and ends before the possible '?'.
978 */
979static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
980{
981 struct channel *chn = SMP_REQ_CHN(smp);
982 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
983 struct htx_sl *sl;
984 struct ist path;
985 size_t len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200986
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200987 if (!htx)
988 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200989
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200990 sl = http_get_stline(htx);
991 path = http_get_path(htx_sl_req_uri(sl));
992 if (!path.ptr)
993 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200994
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200995 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
996 ;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200997
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200998 /* OK, we got the '/' ! */
999 smp->data.type = SMP_T_STR;
1000 smp->data.u.str.area = path.ptr;
1001 smp->data.u.str.data = len;
1002 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001003 return 1;
1004}
1005
1006/* This produces a concatenation of the first occurrence of the Host header
1007 * followed by the path component if it begins with a slash ('/'). This means
1008 * that '*' will not be added, resulting in exactly the first Host entry.
1009 * If no Host header is found, then the path is returned as-is. The returned
1010 * value is stored in the trash so it does not need to be marked constant.
1011 * The returned sample is of type string.
1012 */
1013static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1014{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001015 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001016 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1017 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001018 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001019 struct http_hdr_ctx ctx;
1020 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001021
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001022 if (!htx)
1023 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001024
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001025 ctx.blk = NULL;
1026 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1027 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001028
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001029 /* OK we have the header value in ctx.value */
1030 temp = get_trash_chunk();
1031 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001032
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001033 /* now retrieve the path */
1034 sl = http_get_stline(htx);
1035 path = http_get_path(htx_sl_req_uri(sl));
1036 if (path.ptr) {
1037 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001038
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001039 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1040 ;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001041
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001042 if (len && *(path.ptr) == '/')
1043 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001044 }
1045
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001046 smp->data.type = SMP_T_STR;
1047 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001048 smp->flags = SMP_F_VOL_1ST;
1049 return 1;
1050}
1051
1052/* This produces a 32-bit hash of the concatenation of the first occurrence of
1053 * the Host header followed by the path component if it begins with a slash ('/').
1054 * This means that '*' will not be added, resulting in exactly the first Host
1055 * entry. If no Host header is found, then the path is used. The resulting value
1056 * is hashed using the path hash followed by a full avalanche hash and provides a
1057 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1058 * high-traffic sites without having to store whole paths.
1059 */
1060static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1061{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001062 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001063 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1064 struct htx_sl *sl;
1065 struct http_hdr_ctx ctx;
1066 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001067 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001068
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001069 if (!htx)
1070 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001071
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001072 ctx.blk = NULL;
1073 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1074 /* OK we have the header value in ctx.value */
1075 while (ctx.value.len--)
1076 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001077 }
1078
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001079 /* now retrieve the path */
1080 sl = http_get_stline(htx);
1081 path = http_get_path(htx_sl_req_uri(sl));
1082 if (path.ptr) {
1083 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001084
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001085 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1086 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001087
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001088 if (len && *(path.ptr) == '/') {
1089 while (len--)
1090 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001091 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001092 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001093
Willy Tarreau79e57332018-10-02 16:01:16 +02001094 hash = full_hash(hash);
1095
1096 smp->data.type = SMP_T_SINT;
1097 smp->data.u.sint = hash;
1098 smp->flags = SMP_F_VOL_1ST;
1099 return 1;
1100}
1101
1102/* This concatenates the source address with the 32-bit hash of the Host and
1103 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1104 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1105 * on the source address length. The path hash is stored before the address so
1106 * that in environments where IPv6 is insignificant, truncating the output to
1107 * 8 bytes would still work.
1108 */
1109static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1110{
1111 struct buffer *temp;
1112 struct connection *cli_conn = objt_conn(smp->sess->origin);
1113
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001114 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001115 return 0;
1116
1117 if (!smp_fetch_base32(args, smp, kw, private))
1118 return 0;
1119
1120 temp = get_trash_chunk();
1121 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1122 temp->data += sizeof(unsigned int);
1123
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001124 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001125 case AF_INET:
1126 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001127 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001128 4);
1129 temp->data += 4;
1130 break;
1131 case AF_INET6:
1132 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001133 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001134 16);
1135 temp->data += 16;
1136 break;
1137 default:
1138 return 0;
1139 }
1140
1141 smp->data.u.str = *temp;
1142 smp->data.type = SMP_T_BIN;
1143 return 1;
1144}
1145
1146/* Extracts the query string, which comes after the question mark '?'. If no
1147 * question mark is found, nothing is returned. Otherwise it returns a sample
1148 * of type string carrying the whole query string.
1149 */
1150static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1151{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001152 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001153 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1154 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001155 char *ptr, *end;
1156
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001157 if (!htx)
1158 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001159
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001160 sl = http_get_stline(htx);
1161 ptr = HTX_SL_REQ_UPTR(sl);
1162 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001163
1164 /* look up the '?' */
1165 do {
1166 if (ptr == end)
1167 return 0;
1168 } while (*ptr++ != '?');
1169
1170 smp->data.type = SMP_T_STR;
1171 smp->data.u.str.area = ptr;
1172 smp->data.u.str.data = end - ptr;
1173 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1174 return 1;
1175}
1176
1177static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1178{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001179 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001180 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001181
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001182 if (!htx)
1183 return 0;
1184 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001185 smp->data.u.sint = 1;
1186 return 1;
1187}
1188
1189/* return a valid test if the current request is the first one on the connection */
1190static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1191{
1192 smp->data.type = SMP_T_BOOL;
1193 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1194 return 1;
1195}
1196
Christopher Fauleta4063562019-08-02 11:51:37 +02001197/* Fetch the authentication method if there is an Authorization header. It
1198 * relies on get_http_auth()
1199 */
1200static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1201{
1202 struct channel *chn = SMP_REQ_CHN(smp);
1203 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1204 struct http_txn *txn;
1205
1206 if (!htx)
1207 return 0;
1208
1209 txn = smp->strm->txn;
1210 if (!get_http_auth(smp, htx))
1211 return 0;
1212
1213 switch (txn->auth.method) {
1214 case HTTP_AUTH_BASIC:
1215 smp->data.u.str.area = "Basic";
1216 smp->data.u.str.data = 5;
1217 break;
1218 case HTTP_AUTH_DIGEST:
1219 /* Unexpected because not supported */
1220 smp->data.u.str.area = "Digest";
1221 smp->data.u.str.data = 6;
1222 break;
1223 default:
1224 return 0;
1225 }
1226
1227 smp->data.type = SMP_T_STR;
1228 smp->flags = SMP_F_CONST;
1229 return 1;
1230}
1231
1232/* Fetch the user supplied if there is an Authorization header. It relies on
1233 * get_http_auth()
1234 */
1235static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1236{
1237 struct channel *chn = SMP_REQ_CHN(smp);
1238 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1239 struct http_txn *txn;
1240
1241 if (!htx)
1242 return 0;
1243
1244 txn = smp->strm->txn;
1245 if (!get_http_auth(smp, htx))
1246 return 0;
1247
1248 smp->data.type = SMP_T_STR;
1249 smp->data.u.str.area = txn->auth.user;
1250 smp->data.u.str.data = strlen(txn->auth.user);
1251 smp->flags = SMP_F_CONST;
1252 return 1;
1253}
1254
1255/* Fetch the password supplied if there is an Authorization header. It relies on
1256 * get_http_auth()
1257 */
1258static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1259{
1260 struct channel *chn = SMP_REQ_CHN(smp);
1261 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1262 struct http_txn *txn;
1263
1264 if (!htx)
1265 return 0;
1266
1267 txn = smp->strm->txn;
1268 if (!get_http_auth(smp, htx))
1269 return 0;
1270
1271 smp->data.type = SMP_T_STR;
1272 smp->data.u.str.area = txn->auth.pass;
1273 smp->data.u.str.data = strlen(txn->auth.pass);
1274 smp->flags = SMP_F_CONST;
1275 return 1;
1276}
1277
Willy Tarreau79e57332018-10-02 16:01:16 +02001278/* Accepts exactly 1 argument of type userlist */
1279static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1280{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001281 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001282 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001283
1284 if (!args || args->type != ARGT_USR)
1285 return 0;
1286
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001287 if (!htx)
1288 return 0;
1289 if (!get_http_auth(smp, htx))
1290 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001291
1292 smp->data.type = SMP_T_BOOL;
1293 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001294 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001295 return 1;
1296}
1297
1298/* Accepts exactly 1 argument of type userlist */
1299static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1300{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001301 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001302 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001303
Willy Tarreau79e57332018-10-02 16:01:16 +02001304 if (!args || args->type != ARGT_USR)
1305 return 0;
1306
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001307 if (!htx)
1308 return 0;
1309 if (!get_http_auth(smp, htx))
1310 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001311
Willy Tarreau79e57332018-10-02 16:01:16 +02001312 /* if the user does not belong to the userlist or has a wrong password,
1313 * report that it unconditionally does not match. Otherwise we return
1314 * a string containing the username.
1315 */
1316 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1317 smp->strm->txn->auth.pass))
1318 return 0;
1319
1320 /* pat_match_auth() will need the user list */
1321 smp->ctx.a[0] = args->data.usr;
1322
1323 smp->data.type = SMP_T_STR;
1324 smp->flags = SMP_F_CONST;
1325 smp->data.u.str.area = smp->strm->txn->auth.user;
1326 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1327
1328 return 1;
1329}
1330
1331/* Fetch a captured HTTP request header. The index is the position of
1332 * the "capture" option in the configuration file
1333 */
1334static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1335{
1336 struct proxy *fe = strm_fe(smp->strm);
1337 int idx;
1338
1339 if (!args || args->type != ARGT_SINT)
1340 return 0;
1341
1342 idx = args->data.sint;
1343
1344 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1345 return 0;
1346
1347 smp->data.type = SMP_T_STR;
1348 smp->flags |= SMP_F_CONST;
1349 smp->data.u.str.area = smp->strm->req_cap[idx];
1350 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1351
1352 return 1;
1353}
1354
1355/* Fetch a captured HTTP response header. The index is the position of
1356 * the "capture" option in the configuration file
1357 */
1358static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1359{
1360 struct proxy *fe = strm_fe(smp->strm);
1361 int idx;
1362
1363 if (!args || args->type != ARGT_SINT)
1364 return 0;
1365
1366 idx = args->data.sint;
1367
1368 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1369 return 0;
1370
1371 smp->data.type = SMP_T_STR;
1372 smp->flags |= SMP_F_CONST;
1373 smp->data.u.str.area = smp->strm->res_cap[idx];
1374 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1375
1376 return 1;
1377}
1378
1379/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1380static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1381{
1382 struct buffer *temp;
1383 struct http_txn *txn = smp->strm->txn;
1384 char *ptr;
1385
1386 if (!txn || !txn->uri)
1387 return 0;
1388
1389 ptr = txn->uri;
1390
1391 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1392 ptr++;
1393
1394 temp = get_trash_chunk();
1395 temp->area = txn->uri;
1396 temp->data = ptr - txn->uri;
1397 smp->data.u.str = *temp;
1398 smp->data.type = SMP_T_STR;
1399 smp->flags = SMP_F_CONST;
1400
1401 return 1;
1402
1403}
1404
1405/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1406static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1407{
1408 struct http_txn *txn = smp->strm->txn;
1409 struct ist path;
1410 const char *ptr;
1411
1412 if (!txn || !txn->uri)
1413 return 0;
1414
1415 ptr = txn->uri;
1416
1417 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1418 ptr++;
1419
1420 if (!*ptr)
1421 return 0;
1422
Christopher Faulet78337bb2018-11-15 14:35:18 +01001423 /* skip the first space and find space after URI */
1424 path = ist2(++ptr, 0);
1425 while (*ptr != ' ' && *ptr != '\0')
1426 ptr++;
1427 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001428
Christopher Faulet78337bb2018-11-15 14:35:18 +01001429 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02001430 if (!path.ptr)
1431 return 0;
1432
1433 smp->data.u.str.area = path.ptr;
1434 smp->data.u.str.data = path.len;
1435 smp->data.type = SMP_T_STR;
1436 smp->flags = SMP_F_CONST;
1437
1438 return 1;
1439}
1440
1441/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1442 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1443 */
1444static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1445{
1446 struct http_txn *txn = smp->strm->txn;
1447
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001448 if (!txn || txn->req.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001449 return 0;
1450
1451 if (txn->req.flags & HTTP_MSGF_VER_11)
1452 smp->data.u.str.area = "HTTP/1.1";
1453 else
1454 smp->data.u.str.area = "HTTP/1.0";
1455
1456 smp->data.u.str.data = 8;
1457 smp->data.type = SMP_T_STR;
1458 smp->flags = SMP_F_CONST;
1459 return 1;
1460
1461}
1462
1463/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1464 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1465 */
1466static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1467{
1468 struct http_txn *txn = smp->strm->txn;
1469
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001470 if (!txn || txn->rsp.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001471 return 0;
1472
1473 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1474 smp->data.u.str.area = "HTTP/1.1";
1475 else
1476 smp->data.u.str.area = "HTTP/1.0";
1477
1478 smp->data.u.str.data = 8;
1479 smp->data.type = SMP_T_STR;
1480 smp->flags = SMP_F_CONST;
1481 return 1;
1482
1483}
1484
1485/* Iterate over all cookies present in a message. The context is stored in
1486 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1487 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1488 * the direction, multiple cookies may be parsed on the same line or not.
1489 * The cookie name is in args and the name length in args->data.str.len.
1490 * Accepts exactly 1 argument of type string. If the input options indicate
1491 * that no iterating is desired, then only last value is fetched if any.
1492 * The returned sample is of type CSTR. Can be used to parse cookies in other
1493 * files.
1494 */
1495static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1496{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001497 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1498 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001499 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1500 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1501 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001502 int occ = 0;
1503 int found = 0;
1504
1505 if (!args || args->type != ARGT_STR)
1506 return 0;
1507
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001508 if (!ctx) {
1509 /* first call */
1510 ctx = &static_http_hdr_ctx;
1511 ctx->blk = NULL;
1512 smp->ctx.a[2] = ctx;
1513 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001514
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001515 if (!htx)
1516 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001517
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001518 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001519
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001520 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1521 /* no explicit occurrence and single fetch => last cookie by default */
1522 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001523
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001524 /* OK so basically here, either we want only one value and it's the
1525 * last one, or we want to iterate over all of them and we fetch the
1526 * next one.
1527 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001528
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001529 if (!(smp->flags & SMP_F_NOT_LAST)) {
1530 /* search for the header from the beginning, we must first initialize
1531 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001532 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001533 smp->ctx.a[0] = NULL;
1534 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001535 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001536
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001537 smp->flags |= SMP_F_VOL_HDR;
1538 while (1) {
1539 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1540 if (!smp->ctx.a[0]) {
1541 if (!http_find_header(htx, hdr, ctx, 0))
1542 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001543
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001544 if (ctx->value.len < args->data.str.data + 1)
1545 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001546
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001547 smp->ctx.a[0] = ctx->value.ptr;
1548 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001549 }
1550
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001551 smp->data.type = SMP_T_STR;
1552 smp->flags |= SMP_F_CONST;
1553 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
1554 args->data.str.area, args->data.str.data,
1555 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1556 &smp->data.u.str.area,
1557 &smp->data.u.str.data);
1558 if (smp->ctx.a[0]) {
1559 found = 1;
1560 if (occ >= 0) {
1561 /* one value was returned into smp->data.u.str.{str,len} */
1562 smp->flags |= SMP_F_NOT_LAST;
1563 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001564 }
1565 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001566 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001567 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001568
Willy Tarreau79e57332018-10-02 16:01:16 +02001569 /* all cookie headers and values were scanned. If we're looking for the
1570 * last occurrence, we may return it now.
1571 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001572 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001573 smp->flags &= ~SMP_F_NOT_LAST;
1574 return found;
1575}
1576
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001577/* Same than smp_fetch_cookie() but only relies on the sample direction to
1578 * choose the right channel. So instead of duplicating the code, we just change
1579 * the keyword and then fallback on smp_fetch_cookie().
1580 */
1581static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1582{
1583 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1584 return smp_fetch_cookie(args, smp, kw, private);
1585}
1586
Willy Tarreau79e57332018-10-02 16:01:16 +02001587/* Iterate over all cookies present in a request to count how many occurrences
1588 * match the name in args and args->data.str.len. If <multi> is non-null, then
1589 * multiple cookies may be parsed on the same line. The returned sample is of
1590 * type UINT. Accepts exactly 1 argument of type string.
1591 */
1592static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1593{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001594 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1595 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001596 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1597 struct http_hdr_ctx ctx;
1598 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001599 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001600 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001601
1602 if (!args || args->type != ARGT_STR)
1603 return 0;
1604
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001605 if (!htx)
1606 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001607
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001608 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001609
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001610 val_end = val_beg = NULL;
1611 ctx.blk = NULL;
1612 cnt = 0;
1613 while (1) {
1614 /* Note: val_beg == NULL every time we need to fetch a new header */
1615 if (!val_beg) {
1616 if (!http_find_header(htx, hdr, &ctx, 0))
1617 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001618
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001619 if (ctx.value.len < args->data.str.data + 1)
1620 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001621
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001622 val_beg = ctx.value.ptr;
1623 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001624 }
1625
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001626 smp->data.type = SMP_T_STR;
1627 smp->flags |= SMP_F_CONST;
1628 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
1629 args->data.str.area, args->data.str.data,
1630 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1631 &smp->data.u.str.area,
1632 &smp->data.u.str.data))) {
1633 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001634 }
1635 }
1636
1637 smp->data.type = SMP_T_SINT;
1638 smp->data.u.sint = cnt;
1639 smp->flags |= SMP_F_VOL_HDR;
1640 return 1;
1641}
1642
1643/* Fetch an cookie's integer value. The integer value is returned. It
1644 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1645 */
1646static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1647{
1648 int ret = smp_fetch_cookie(args, smp, kw, private);
1649
1650 if (ret > 0) {
1651 smp->data.type = SMP_T_SINT;
1652 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1653 smp->data.u.str.data);
1654 }
1655
1656 return ret;
1657}
1658
1659/************************************************************************/
1660/* The code below is dedicated to sample fetches */
1661/************************************************************************/
1662
1663/* This scans a URL-encoded query string. It takes an optionally wrapping
1664 * string whose first contigous chunk has its beginning in ctx->a[0] and end
1665 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1666 * pointers are updated for next iteration before leaving.
1667 */
1668static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1669{
1670 const char *vstart, *vend;
1671 struct buffer *temp;
1672 const char **chunks = (const char **)smp->ctx.a;
1673
1674 if (!http_find_next_url_param(chunks, name, name_len,
1675 &vstart, &vend, delim))
1676 return 0;
1677
1678 /* Create sample. If the value is contiguous, return the pointer as CONST,
1679 * if the value is wrapped, copy-it in a buffer.
1680 */
1681 smp->data.type = SMP_T_STR;
1682 if (chunks[2] &&
1683 vstart >= chunks[0] && vstart <= chunks[1] &&
1684 vend >= chunks[2] && vend <= chunks[3]) {
1685 /* Wrapped case. */
1686 temp = get_trash_chunk();
1687 memcpy(temp->area, vstart, chunks[1] - vstart);
1688 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1689 vend - chunks[2]);
1690 smp->data.u.str.area = temp->area;
1691 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1692 } else {
1693 /* Contiguous case. */
1694 smp->data.u.str.area = (char *)vstart;
1695 smp->data.u.str.data = vend - vstart;
1696 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1697 }
1698
1699 /* Update context, check wrapping. */
1700 chunks[0] = vend;
1701 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1702 chunks[1] = chunks[3];
1703 chunks[2] = NULL;
1704 }
1705
1706 if (chunks[0] < chunks[1])
1707 smp->flags |= SMP_F_NOT_LAST;
1708
1709 return 1;
1710}
1711
1712/* This function iterates over each parameter of the query string. It uses
1713 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1714 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1715 * An optional parameter name is passed in args[0], otherwise any parameter is
1716 * considered. It supports an optional delimiter argument for the beginning of
1717 * the string in args[1], which defaults to "?".
1718 */
1719static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1720{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001721 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001722 char delim = '?';
1723 const char *name;
1724 int name_len;
1725
1726 if (!args ||
1727 (args[0].type && args[0].type != ARGT_STR) ||
1728 (args[1].type && args[1].type != ARGT_STR))
1729 return 0;
1730
1731 name = "";
1732 name_len = 0;
1733 if (args->type == ARGT_STR) {
1734 name = args->data.str.area;
1735 name_len = args->data.str.data;
1736 }
1737
1738 if (args[1].type)
1739 delim = *args[1].data.str.area;
1740
1741 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001742 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1743 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001744
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001745 if (!htx)
1746 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001747
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001748 sl = http_get_stline(htx);
1749 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1750 if (!smp->ctx.a[0])
1751 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001752
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001753 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001754
1755 /* Assume that the context is filled with NULL pointer
1756 * before the first call.
1757 * smp->ctx.a[2] = NULL;
1758 * smp->ctx.a[3] = NULL;
1759 */
1760 }
1761
1762 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1763}
1764
1765/* This function iterates over each parameter of the body. This requires
1766 * that the body has been waited for using http-buffer-request. It uses
1767 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
1768 * contigous part of the body, and optionally ctx->a[2..3] to reference the
1769 * optional second part if the body wraps at the end of the buffer. An optional
1770 * parameter name is passed in args[0], otherwise any parameter is considered.
1771 */
1772static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1773{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001774 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001775 const char *name;
1776 int name_len;
1777
1778 if (!args || (args[0].type && args[0].type != ARGT_STR))
1779 return 0;
1780
1781 name = "";
1782 name_len = 0;
1783 if (args[0].type == ARGT_STR) {
1784 name = args[0].data.str.area;
1785 name_len = args[0].data.str.data;
1786 }
1787
1788 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001789 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1790 struct buffer *temp;
1791 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001792
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001793 if (!htx)
1794 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001795
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001796 temp = get_trash_chunk();
1797 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1798 struct htx_blk *blk = htx_get_blk(htx, pos);
1799 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001800
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001801 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1802 break;
1803 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001804 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001805 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001806 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001807 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001808
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001809 smp->ctx.a[0] = temp->area;
1810 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001811
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001812 /* Assume that the context is filled with NULL pointer
1813 * before the first call.
1814 * smp->ctx.a[2] = NULL;
1815 * smp->ctx.a[3] = NULL;
1816 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001817
Willy Tarreau79e57332018-10-02 16:01:16 +02001818 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001819
Willy Tarreau79e57332018-10-02 16:01:16 +02001820 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1821}
1822
1823/* Return the signed integer value for the specified url parameter (see url_param
1824 * above).
1825 */
1826static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1827{
1828 int ret = smp_fetch_url_param(args, smp, kw, private);
1829
1830 if (ret > 0) {
1831 smp->data.type = SMP_T_SINT;
1832 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1833 smp->data.u.str.data);
1834 }
1835
1836 return ret;
1837}
1838
1839/* This produces a 32-bit hash of the concatenation of the first occurrence of
1840 * the Host header followed by the path component if it begins with a slash ('/').
1841 * This means that '*' will not be added, resulting in exactly the first Host
1842 * entry. If no Host header is found, then the path is used. The resulting value
1843 * is hashed using the url hash followed by a full avalanche hash and provides a
1844 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1845 * high-traffic sites without having to store whole paths.
1846 * this differs from the base32 functions in that it includes the url parameters
1847 * as well as the path
1848 */
1849static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1850{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001851 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001852 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1853 struct http_hdr_ctx ctx;
1854 struct htx_sl *sl;
1855 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001856 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001857
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001858 if (!htx)
1859 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001860
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001861 ctx.blk = NULL;
1862 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1863 /* OK we have the header value in ctx.value */
1864 while (ctx.value.len--)
1865 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001866 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001867
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001868 /* now retrieve the path */
1869 sl = http_get_stline(htx);
1870 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001871 if (path.len && *(path.ptr) == '/') {
1872 while (path.len--)
1873 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001874 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001875
Willy Tarreau79e57332018-10-02 16:01:16 +02001876 hash = full_hash(hash);
1877
1878 smp->data.type = SMP_T_SINT;
1879 smp->data.u.sint = hash;
1880 smp->flags = SMP_F_VOL_1ST;
1881 return 1;
1882}
1883
1884/* This concatenates the source address with the 32-bit hash of the Host and
1885 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1886 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1887 * on the source address length. The URL hash is stored before the address so
1888 * that in environments where IPv6 is insignificant, truncating the output to
1889 * 8 bytes would still work.
1890 */
1891static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1892{
1893 struct buffer *temp;
1894 struct connection *cli_conn = objt_conn(smp->sess->origin);
1895
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001896 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001897 return 0;
1898
1899 if (!smp_fetch_url32(args, smp, kw, private))
1900 return 0;
1901
1902 temp = get_trash_chunk();
1903 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1904 temp->data += sizeof(unsigned int);
1905
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001906 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001907 case AF_INET:
1908 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001909 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001910 4);
1911 temp->data += 4;
1912 break;
1913 case AF_INET6:
1914 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001915 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001916 16);
1917 temp->data += 16;
1918 break;
1919 default:
1920 return 0;
1921 }
1922
1923 smp->data.u.str = *temp;
1924 smp->data.type = SMP_T_BIN;
1925 return 1;
1926}
1927
1928/************************************************************************/
1929/* Other utility functions */
1930/************************************************************************/
1931
1932/* This function is used to validate the arguments passed to any "hdr" fetch
1933 * keyword. These keywords support an optional positive or negative occurrence
1934 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
1935 * is assumed that the types are already the correct ones. Returns 0 on error,
1936 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
1937 * error message in case of error, that the caller is responsible for freeing.
1938 * The initial location must either be freeable or NULL.
1939 * Note: this function's pointer is checked from Lua.
1940 */
1941int val_hdr(struct arg *arg, char **err_msg)
1942{
1943 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
1944 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
1945 return 0;
1946 }
1947 return 1;
1948}
1949
1950/************************************************************************/
1951/* All supported sample fetch keywords must be declared here. */
1952/************************************************************************/
1953
1954/* Note: must not be declared <const> as its list will be overwritten */
1955static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
1956 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1957 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
1958 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
1959
1960 /* capture are allocated and are permanent in the stream */
1961 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
1962
1963 /* retrieve these captures from the HTTP logs */
1964 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1965 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1966 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1967
1968 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
1969 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1970
1971 /* cookie is valid in both directions (eg: for "stick ...") but cook*
1972 * are only here to match the ACL's name, are request-only and are used
1973 * for ACL compatibility only.
1974 */
1975 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001976 { "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 +02001977 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1978 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1979
1980 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
1981 * only here to match the ACL's name, are request-only and are used for
1982 * ACL compatibility only.
1983 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001984 { "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 +02001985 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1986 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
1987 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
1988
Christopher Fauleta4063562019-08-02 11:51:37 +02001989 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1990 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1991 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02001992 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
1993 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
1994 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
1995 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
1996 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1997 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1998
1999 /* HTTP protocol on the request path */
2000 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2001 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2002
2003 /* HTTP version on the request path */
2004 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2005 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2006
2007 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2008 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2009 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2010 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2011
2012 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2013 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2014
2015 /* HTTP version on the response path */
2016 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2017 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2018
2019 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2020 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2021 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2022 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2023
2024 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2025 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2026 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2027 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2028 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2029 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2030 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2031
2032 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2033 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2034 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2035 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2036
2037 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2038 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2039 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2040 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2041 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2042 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2043 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2044
2045 /* scook is valid only on the response and is used for ACL compatibility */
2046 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2047 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2048 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2049 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2050
2051 /* shdr is valid only on the response and is used for ACL compatibility */
2052 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2053 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2054 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2055 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2056
2057 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2058 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2059 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2060 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2061 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2062 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2063 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2064 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2065 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2066 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2067 { /* END */ },
2068}};
2069
Willy Tarreau0108d902018-11-25 19:14:37 +01002070INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002071
2072/*
2073 * Local variables:
2074 * c-indent-level: 8
2075 * c-basic-offset: 8
2076 * End:
2077 */