blob: df1ca3318bcbfbf83496a83d9078912097fe2552 [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 Fauletef453ed2018-10-24 21:39:27 +020039#include <proto/http_htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020040#include <proto/log.h>
41#include <proto/obj_type.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020042#include <proto/http_ana.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020043#include <proto/sample.h>
44#include <proto/stream.h>
45
46
47/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Fauletef453ed2018-10-24 21:39:27 +020048static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
49
Christopher Faulet89dc4992019-04-17 12:02:59 +020050#define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL)
51#define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL)
Willy Tarreau79e57332018-10-02 16:01:16 +020052
53/*
54 * Returns the data from Authorization header. Function may be called more
55 * than once so data is stored in txn->auth_data. When no header is found
56 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
57 * searching again for something we are unable to find anyway. However, if
58 * the result if valid, the cache is not reused because we would risk to
59 * have the credentials overwritten by another stream in parallel.
60 */
61
Christopher Fauletcd761952019-07-15 13:58:29 +020062static int get_http_auth(struct sample *smp, struct htx *htx)
Willy Tarreau79e57332018-10-02 16:01:16 +020063{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020064 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020065 struct http_txn *txn = s->txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020066 struct http_hdr_ctx ctx = { .blk = NULL };
67 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020068 struct buffer auth_method;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020069 char *p;
Willy Tarreau79e57332018-10-02 16:01:16 +020070 int len;
71
72#ifdef DEBUG_AUTH
73 printf("Auth for stream %p: %d\n", s, txn->auth.method);
74#endif
Willy Tarreau79e57332018-10-02 16:01:16 +020075 if (txn->auth.method == HTTP_AUTH_WRONG)
76 return 0;
77
78 txn->auth.method = HTTP_AUTH_WRONG;
79
Christopher Faulet6d1dd462019-07-15 14:36:03 +020080 if (txn->flags & TX_USE_PX_CONN)
81 hdr = ist("Proxy-Authorization");
82 else
83 hdr = ist("Authorization");
Willy Tarreau79e57332018-10-02 16:01:16 +020084
Christopher Faulet6d1dd462019-07-15 14:36:03 +020085 ctx.blk = NULL;
86 if (!http_find_header(htx, hdr, &ctx, 0))
87 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +020088
Christopher Faulet6d1dd462019-07-15 14:36:03 +020089 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
90 len = p - ctx.value.ptr;
91 if (!p || len <= 0)
92 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +020093
Christopher Faulet6d1dd462019-07-15 14:36:03 +020094 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
95 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +020096
Christopher Faulet6d1dd462019-07-15 14:36:03 +020097 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +020098
99 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
100 struct buffer *http_auth = get_trash_chunk();
101
102 len = base64dec(txn->auth.method_data.area,
103 txn->auth.method_data.data,
104 http_auth->area, global.tune.bufsize - 1);
105
106 if (len < 0)
107 return 0;
108
109
110 http_auth->area[len] = '\0';
111
112 p = strchr(http_auth->area, ':');
113
114 if (!p)
115 return 0;
116
117 txn->auth.user = http_auth->area;
118 *p = '\0';
119 txn->auth.pass = p+1;
120
121 txn->auth.method = HTTP_AUTH_BASIC;
122 return 1;
123 }
124
125 return 0;
126}
127
128/* This function ensures that the prerequisites for an L7 fetch are ready,
129 * which means that a request or response is ready. If some data is missing,
130 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200131 * to extract data from L7. If <vol> is non-null during a prefetch, another
132 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200133 *
134 * The function returns :
135 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
136 * decide whether or not an HTTP message is present ;
137 * NULL if the requested data cannot be fetched or if it is certain that
138 * we'll never have any HTTP message there ;
139 * The HTX message if ready
140 */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200141struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200142{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200143 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200144 struct http_txn *txn = NULL;
145 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200146 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100147 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200148
149 /* Note: it is possible that <s> is NULL when called before stream
150 * initialization (eg: tcp-request connection), so this function is the
151 * one responsible for guarding against this case for all HTTP users.
152 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200153 if (!s || !chn)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200154 return NULL;
155
156 if (!s->txn) {
157 if (unlikely(!http_alloc_txn(s)))
158 return NULL; /* not enough memory */
159 http_init_txn(s);
160 txn = s->txn;
161 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200162 txn = s->txn;
163 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
164 smp->data.type = SMP_T_BOOL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200165
Christopher Fauleteca88542019-04-03 10:12:42 +0200166 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200167 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200168
Christopher Faulet89dc4992019-04-17 12:02:59 +0200169 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
170 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200171
Christopher Faulet89dc4992019-04-17 12:02:59 +0200172 if (msg->msg_state < HTTP_MSG_BODY) {
173 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200174 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200175 /* Parsing is done by the mux, just wait */
176 smp->flags |= SMP_F_MAY_CHANGE;
177 return NULL;
178 }
179 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200180 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200181 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200182 /* The start-line was already forwarded, it is too late to fetch anything */
183 return NULL;
184 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200185 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200186 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200187 struct buffer *buf;
188 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200189 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200190 union h1_sl h1sl;
191 unsigned int flags = HTX_FL_NONE;
192 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200193
Christopher Faulet89dc4992019-04-17 12:02:59 +0200194 /* no HTTP fetch on the response in TCP mode */
195 if (chn->flags & CF_ISRESP)
196 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200197
Christopher Faulet89dc4992019-04-17 12:02:59 +0200198 /* Now we are working on the request only */
199 buf = &chn->buf;
200 if (b_head(buf) + b_data(buf) > b_wrap(buf))
201 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200202
Christopher Faulet89dc4992019-04-17 12:02:59 +0200203 h1m_init_req(&h1m);
204 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
205 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
206 if (ret <= 0) {
207 /* Invalid or too big*/
208 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200209 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100210
Christopher Faulet89dc4992019-04-17 12:02:59 +0200211 /* wait for a full request */
212 smp->flags |= SMP_F_MAY_CHANGE;
213 return NULL;
214 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100215
Christopher Faulet89dc4992019-04-17 12:02:59 +0200216 /* OK we just got a valid HTTP mesage. We have to convert it
217 * into an HTX message.
218 */
219 if (unlikely(h1sl.rq.v.len == 0)) {
220 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
221 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200222 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200223 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200224 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200225
226 /* Set HTX start-line flags */
227 if (h1m.flags & H1_MF_VER_11)
228 flags |= HTX_SL_F_VER_11;
229 if (h1m.flags & H1_MF_XFER_ENC)
230 flags |= HTX_SL_F_XFER_ENC;
231 flags |= HTX_SL_F_XFER_LEN;
232 if (h1m.flags & H1_MF_CHNK)
233 flags |= HTX_SL_F_CHNK;
234 else if (h1m.flags & H1_MF_CLEN)
235 flags |= HTX_SL_F_CLEN;
236
237 htx = htx_from_buf(get_trash_chunk());
238 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
239 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200240 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200241 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200242 }
243
244 /* OK we just got a valid HTTP message. If not already done by
245 * HTTP analyzers, we have some minor preparation to perform so
246 * that further checks can rely on HTTP tests.
247 */
248 if (sl && msg->msg_state < HTTP_MSG_BODY) {
249 if (!(chn->flags & CF_ISRESP)) {
250 txn->meth = sl->info.req.meth;
251 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
252 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200253 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200254 else
255 txn->status = sl->info.res.status;
256 if (sl->flags & HTX_SL_F_VER_11)
257 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200258 }
259
260 /* everything's OK */
261 smp->data.u.sint = 1;
262 return htx;
263}
264
Willy Tarreau79e57332018-10-02 16:01:16 +0200265/* This function fetches the method of current HTTP request and stores
266 * it in the global pattern struct as a chunk. There are two possibilities :
267 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
268 * in <len> and <ptr> is NULL ;
269 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
270 * <len> to its length.
271 * This is intended to be used with pat_match_meth() only.
272 */
273static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
274{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200275 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200276 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +0200277 struct http_txn *txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200278 int meth;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200279
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200280 if (!htx)
281 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200282
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200283 txn = smp->strm->txn;
284 meth = txn->meth;
285 smp->data.type = SMP_T_METH;
286 smp->data.u.meth.meth = meth;
287 if (meth == HTTP_METH_OTHER) {
288 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200289
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200290 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
291 /* ensure the indexes are not affected */
292 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200293 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200294 sl = http_get_stline(htx);
295 smp->flags |= SMP_F_CONST;
296 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
297 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200298 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200299 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200300 return 1;
301}
302
303static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
304{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200305 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200306 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
307 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200308 char *ptr;
309 int len;
310
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 sl = http_get_stline(htx);
315 len = HTX_SL_REQ_VLEN(sl);
316 ptr = HTX_SL_REQ_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200317
318 while ((len-- > 0) && (*ptr++ != '/'));
319 if (len <= 0)
320 return 0;
321
322 smp->data.type = SMP_T_STR;
323 smp->data.u.str.area = ptr;
324 smp->data.u.str.data = len;
325
326 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
327 return 1;
328}
329
330static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
331{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200332 struct channel *chn = SMP_RES_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200333 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
334 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200335 char *ptr;
336 int len;
337
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200338 if (!htx)
339 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200340
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200341 sl = http_get_stline(htx);
342 len = HTX_SL_RES_VLEN(sl);
343 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200344
345 while ((len-- > 0) && (*ptr++ != '/'));
346 if (len <= 0)
347 return 0;
348
349 smp->data.type = SMP_T_STR;
350 smp->data.u.str.area = ptr;
351 smp->data.u.str.data = len;
352
353 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
354 return 1;
355}
356
357/* 3. Check on Status Code. We manipulate integers here. */
358static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
359{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200360 struct channel *chn = SMP_RES_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200361 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
362 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200363 char *ptr;
364 int len;
365
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200366 if (!htx)
367 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200368
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200369 sl = http_get_stline(htx);
370 len = HTX_SL_RES_CLEN(sl);
371 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200372
373 smp->data.type = SMP_T_SINT;
374 smp->data.u.sint = __strl2ui(ptr, len);
375 smp->flags = SMP_F_VOL_1ST;
376 return 1;
377}
378
379static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
380{
381 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
382 return 0;
383
384 if (!smp->strm->unique_id) {
385 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
386 return 0;
387 smp->strm->unique_id[0] = '\0';
388 }
389 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
390 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
391
392 smp->data.type = SMP_T_STR;
393 smp->data.u.str.area = smp->strm->unique_id;
394 smp->flags = SMP_F_CONST;
395 return 1;
396}
397
398/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800399 * empty line which separes headers from the body. This is useful
400 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200401 */
402static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
403{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200404 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200405 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
406 struct buffer *temp;
407 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200408
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200409 if (!htx)
410 return 0;
411 temp = get_trash_chunk();
412 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
413 struct htx_blk *blk = htx_get_blk(htx, pos);
414 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200415
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200416 if (type == HTX_BLK_HDR) {
417 struct ist n = htx_get_blk_name(htx, blk);
418 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200419
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200420 if (!htx_hdr_to_h1(n, v, temp))
421 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200422 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200423 else if (type == HTX_BLK_EOH) {
424 if (!chunk_memcat(temp, "\r\n", 2))
425 return 0;
426 break;
427 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200428 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200429 smp->data.type = SMP_T_STR;
430 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200431 return 1;
432}
433
434/* Returns the header request in a length/value encoded format.
435 * This is useful for exchanges with the SPOE.
436 *
437 * A "length value" is a multibyte code encoding numbers. It uses the
438 * SPOE format. The encoding is the following:
439 *
440 * Each couple "header name" / "header value" is composed
441 * like this:
442 * "length value" "header name bytes"
443 * "length value" "header value bytes"
444 * When the last header is reached, the header name and the header
445 * value are empty. Their length are 0
446 */
447static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
448{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200449 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200450 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200451 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200452 char *p, *end;
453 int32_t pos;
454 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200455
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200456 if (!htx)
457 return 0;
458 temp = get_trash_chunk();
459 p = temp->area;
460 end = temp->area + temp->size;
461 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
462 struct htx_blk *blk = htx_get_blk(htx, pos);
463 enum htx_blk_type type = htx_get_blk_type(blk);
464 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200465
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200466 if (type == HTX_BLK_HDR) {
467 n = htx_get_blk_name(htx,blk);
468 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200469
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200470 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200471 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200472 if (ret == -1)
473 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200474 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200475 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200476 memcpy(p, n.ptr, n.len);
477 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200478
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200479 /* encode the header value. */
480 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200481 if (ret == -1)
482 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200483 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200484 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200485 memcpy(p, v.ptr, v.len);
486 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200487
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200488 }
489 else if (type == HTX_BLK_EOH) {
490 /* encode the end of the header list with empty
491 * header name and header value.
492 */
493 ret = encode_varint(0, &p, end);
494 if (ret == -1)
495 return 0;
496 ret = encode_varint(0, &p, end);
497 if (ret == -1)
498 return 0;
499 break;
500 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200501 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200502
503 /* Initialise sample data which will be filled. */
504 smp->data.type = SMP_T_BIN;
505 smp->data.u.str.area = temp->area;
506 smp->data.u.str.data = p - temp->area;
507 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200508 return 1;
509}
510
511/* returns the longest available part of the body. This requires that the body
512 * has been waited for using http-buffer-request.
513 */
514static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
515{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200516 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200517 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200518 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200519 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200520
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200521 if (!htx)
522 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200523
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200524 temp = get_trash_chunk();
525 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
526 struct htx_blk *blk = htx_get_blk(htx, pos);
527 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200528
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200529 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
530 break;
531 if (type == HTX_BLK_DATA) {
532 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
533 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200534 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200535 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200536
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200537 smp->data.type = SMP_T_BIN;
538 smp->data.u.str = *temp;
539 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200540 return 1;
541}
542
543
544/* returns the available length of the body. This requires that the body
545 * has been waited for using http-buffer-request.
546 */
547static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
548{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200549 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200550 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
551 int32_t pos;
552 unsigned long long len = 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100553
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200554 if (!htx)
555 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100556
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200557 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
558 struct htx_blk *blk = htx_get_blk(htx, pos);
559 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100560
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200561 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
562 break;
563 if (type == HTX_BLK_DATA)
564 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200565 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200566
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200567 smp->data.type = SMP_T_SINT;
568 smp->data.u.sint = len;
569 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200570 return 1;
571}
572
573
574/* returns the advertised length of the body, or the advertised size of the
575 * chunks available in the buffer. This requires that the body has been waited
576 * for using http-buffer-request.
577 */
578static int smp_fetch_body_size(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 Faulet89dc4992019-04-17 12:02:59 +0200584
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 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200597 if (htx->extra != ULLONG_MAX)
598 len += htx->extra;
Willy Tarreau79e57332018-10-02 16:01:16 +0200599
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200600 smp->data.type = SMP_T_SINT;
601 smp->data.u.sint = len;
602 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200603 return 1;
604}
605
606
607/* 4. Check on URL/URI. A pointer to the URI is stored. */
608static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
609{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200610 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200611 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
612 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200613
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200614 if (!htx)
615 return 0;
616 sl = http_get_stline(htx);
617 smp->data.type = SMP_T_STR;
618 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
619 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
620 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200621 return 1;
622}
623
624static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
625{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200626 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200627 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
628 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200629 struct sockaddr_storage addr;
630
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200631 if (!htx)
632 return 0;
633 sl = http_get_stline(htx);
634 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +0200635
Willy Tarreau79e57332018-10-02 16:01:16 +0200636 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
637 return 0;
638
639 smp->data.type = SMP_T_IPV4;
640 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
641 smp->flags = 0;
642 return 1;
643}
644
645static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
646{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200647 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200648 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
649 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200650 struct sockaddr_storage addr;
651
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200652 if (!htx)
653 return 0;
654 sl = http_get_stline(htx);
655 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200656
Willy Tarreau79e57332018-10-02 16:01:16 +0200657 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
658 return 0;
659
660 smp->data.type = SMP_T_SINT;
661 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
662 smp->flags = 0;
663 return 1;
664}
665
666/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
667 * Accepts an optional argument of type string containing the header field name,
668 * and an optional argument of type signed or unsigned integer to request an
669 * explicit occurrence of the header. Note that in the event of a missing name,
670 * headers are considered from the first one. It does not stop on commas and
671 * returns full lines instead (useful for User-Agent or Date for example).
672 */
673static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
674{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200675 /* possible keywords: req.fhdr, res.fhdr */
676 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200677 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
678 struct http_hdr_ctx *ctx = smp->ctx.a[0];
679 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200680 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200681
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200682 if (!ctx) {
683 /* first call */
684 ctx = &static_http_hdr_ctx;
685 ctx->blk = NULL;
686 smp->ctx.a[0] = ctx;
687 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200688
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200689 if (args) {
690 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200691 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200692 name.ptr = args[0].data.str.area;
693 name.len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +0200694
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200695 if (args[1].type == ARGT_SINT)
696 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200697 }
698
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200699 if (!htx)
700 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200701
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200702 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
703 /* search for header from the beginning */
704 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200705
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200706 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
707 /* no explicit occurrence and single fetch => last header by default */
708 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200709
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200710 if (!occ)
711 /* prepare to report multiple occurrences for ACL fetches */
712 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200713
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200714 smp->data.type = SMP_T_STR;
715 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
716 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
717 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200718 smp->flags &= ~SMP_F_NOT_LAST;
719 return 0;
720}
721
722/* 6. Check on HTTP header count. The number of occurrences is returned.
723 * Accepts exactly 1 argument of type string. It does not stop on commas and
724 * returns full lines instead (useful for User-Agent or Date for example).
725 */
726static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
727{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200728 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
729 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200730 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
731 struct http_hdr_ctx ctx;
732 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200733 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200734
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200735 if (!htx)
736 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200737
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200738 if (args && args->type == ARGT_STR) {
739 name.ptr = args->data.str.area;
740 name.len = args->data.str.data;
741 } else {
742 name.ptr = NULL;
743 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200744 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200745
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200746 ctx.blk = NULL;
747 cnt = 0;
748 while (http_find_header(htx, name, &ctx, 1))
749 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200750 smp->data.type = SMP_T_SINT;
751 smp->data.u.sint = cnt;
752 smp->flags = SMP_F_VOL_HDR;
753 return 1;
754}
755
756static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
757{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200758 /* possible keywords: req.hdr_names, res.hdr_names */
759 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200760 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200761 struct buffer *temp;
762 char del = ',';
763
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200764 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200765
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200766 if (!htx)
767 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200768
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200769 if (args && args->type == ARGT_STR)
770 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200771
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200772 temp = get_trash_chunk();
773 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
774 struct htx_blk *blk = htx_get_blk(htx, pos);
775 enum htx_blk_type type = htx_get_blk_type(blk);
776 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200777
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200778 if (type == HTX_BLK_EOH)
779 break;
780 if (type != HTX_BLK_HDR)
781 continue;
782 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200783
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200784 if (temp->data)
785 temp->area[temp->data++] = del;
786 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200787 }
788
789 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200790 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200791 smp->flags = SMP_F_VOL_HDR;
792 return 1;
793}
794
795/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
796 * Accepts an optional argument of type string containing the header field name,
797 * and an optional argument of type signed or unsigned integer to request an
798 * explicit occurrence of the header. Note that in the event of a missing name,
799 * headers are considered from the first one.
800 */
801static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
802{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200803 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
804 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200805 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
806 struct http_hdr_ctx *ctx = smp->ctx.a[0];
807 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200808 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200809
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200810 if (!ctx) {
811 /* first call */
812 ctx = &static_http_hdr_ctx;
813 ctx->blk = NULL;
814 smp->ctx.a[0] = ctx;
815 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200816
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200817 if (args) {
818 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200819 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200820 name.ptr = args[0].data.str.area;
821 name.len = args[0].data.str.data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200822
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200823 if (args[1].type == ARGT_SINT)
824 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200825 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200826
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200827 if (!htx)
828 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200829
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200830 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
831 /* search for header from the beginning */
832 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200833
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200834 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
835 /* no explicit occurrence and single fetch => last header by default */
836 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200837
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200838 if (!occ)
839 /* prepare to report multiple occurrences for ACL fetches */
840 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200841
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200842 smp->data.type = SMP_T_STR;
843 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
844 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
845 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200846
847 smp->flags &= ~SMP_F_NOT_LAST;
848 return 0;
849}
850
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200851/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
852 * the right channel. So instead of duplicating the code, we just change the
853 * keyword and then fallback on smp_fetch_hdr().
854 */
855static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
856{
857 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
858 return smp_fetch_hdr(args, smp, kw, private);
859}
860
Willy Tarreau79e57332018-10-02 16:01:16 +0200861/* 6. Check on HTTP header count. The number of occurrences is returned.
862 * Accepts exactly 1 argument of type string.
863 */
864static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
865{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200866 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
867 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200868 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
869 struct http_hdr_ctx ctx;
870 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200871 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200872
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200873 if (!htx)
874 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200875
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200876 if (args && args->type == ARGT_STR) {
877 name.ptr = args->data.str.area;
878 name.len = args->data.str.data;
879 } else {
880 name.ptr = NULL;
881 name.len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200882 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200883
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200884 ctx.blk = NULL;
885 cnt = 0;
886 while (http_find_header(htx, name, &ctx, 0))
887 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200888
889 smp->data.type = SMP_T_SINT;
890 smp->data.u.sint = cnt;
891 smp->flags = SMP_F_VOL_HDR;
892 return 1;
893}
894
895/* Fetch an HTTP header's integer value. The integer value is returned. It
896 * takes a mandatory argument of type string and an optional one of type int
897 * to designate a specific occurrence. It returns an unsigned integer, which
898 * may or may not be appropriate for everything.
899 */
900static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
901{
902 int ret = smp_fetch_hdr(args, smp, kw, private);
903
904 if (ret > 0) {
905 smp->data.type = SMP_T_SINT;
906 smp->data.u.sint = strl2ic(smp->data.u.str.area,
907 smp->data.u.str.data);
908 }
909
910 return ret;
911}
912
913/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
914 * and an optional one of type int to designate a specific occurrence.
915 * It returns an IPv4 or IPv6 address.
916 */
917static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
918{
919 int ret;
920
921 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
922 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
923 smp->data.type = SMP_T_IPV4;
924 break;
925 } else {
926 struct buffer *temp = get_trash_chunk();
927 if (smp->data.u.str.data < temp->size - 1) {
928 memcpy(temp->area, smp->data.u.str.area,
929 smp->data.u.str.data);
930 temp->area[smp->data.u.str.data] = '\0';
931 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
932 smp->data.type = SMP_T_IPV6;
933 break;
934 }
935 }
936 }
937
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200938 /* if the header doesn't match an IP address, fetch next one */
939 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200940 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200941 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200942 return ret;
943}
Willy Tarreau79e57332018-10-02 16:01:16 +0200944
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200945/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
946 * the first '/' after the possible hostname, and ends before the possible '?'.
947 */
948static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
949{
950 struct channel *chn = SMP_REQ_CHN(smp);
951 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
952 struct htx_sl *sl;
953 struct ist path;
954 size_t len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200955
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200956 if (!htx)
957 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200958
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200959 sl = http_get_stline(htx);
960 path = http_get_path(htx_sl_req_uri(sl));
961 if (!path.ptr)
962 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200963
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200964 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
965 ;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200966
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200967 /* OK, we got the '/' ! */
968 smp->data.type = SMP_T_STR;
969 smp->data.u.str.area = path.ptr;
970 smp->data.u.str.data = len;
971 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200972 return 1;
973}
974
975/* This produces a concatenation of the first occurrence of the Host header
976 * followed by the path component if it begins with a slash ('/'). This means
977 * that '*' will not be added, resulting in exactly the first Host entry.
978 * If no Host header is found, then the path is returned as-is. The returned
979 * value is stored in the trash so it does not need to be marked constant.
980 * The returned sample is of type string.
981 */
982static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
983{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200984 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200985 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
986 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200987 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200988 struct http_hdr_ctx ctx;
989 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200990
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200991 if (!htx)
992 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200993
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200994 ctx.blk = NULL;
995 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
996 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200997
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200998 /* OK we have the header value in ctx.value */
999 temp = get_trash_chunk();
1000 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001001
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001002 /* now retrieve the path */
1003 sl = http_get_stline(htx);
1004 path = http_get_path(htx_sl_req_uri(sl));
1005 if (path.ptr) {
1006 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001007
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001008 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1009 ;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001010
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001011 if (len && *(path.ptr) == '/')
1012 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001013 }
1014
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001015 smp->data.type = SMP_T_STR;
1016 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001017 smp->flags = SMP_F_VOL_1ST;
1018 return 1;
1019}
1020
1021/* This produces a 32-bit hash of the concatenation of the first occurrence of
1022 * the Host header followed by the path component if it begins with a slash ('/').
1023 * This means that '*' will not be added, resulting in exactly the first Host
1024 * entry. If no Host header is found, then the path is used. The resulting value
1025 * is hashed using the path hash followed by a full avalanche hash and provides a
1026 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1027 * high-traffic sites without having to store whole paths.
1028 */
1029static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1030{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001031 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001032 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1033 struct htx_sl *sl;
1034 struct http_hdr_ctx ctx;
1035 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001036 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001037
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001038 if (!htx)
1039 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001040
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001041 ctx.blk = NULL;
1042 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1043 /* OK we have the header value in ctx.value */
1044 while (ctx.value.len--)
1045 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001046 }
1047
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001048 /* now retrieve the path */
1049 sl = http_get_stline(htx);
1050 path = http_get_path(htx_sl_req_uri(sl));
1051 if (path.ptr) {
1052 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001053
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001054 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1055 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001056
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001057 if (len && *(path.ptr) == '/') {
1058 while (len--)
1059 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001060 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001061 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001062
Willy Tarreau79e57332018-10-02 16:01:16 +02001063 hash = full_hash(hash);
1064
1065 smp->data.type = SMP_T_SINT;
1066 smp->data.u.sint = hash;
1067 smp->flags = SMP_F_VOL_1ST;
1068 return 1;
1069}
1070
1071/* This concatenates the source address with the 32-bit hash of the Host and
1072 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1073 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1074 * on the source address length. The path hash is stored before the address so
1075 * that in environments where IPv6 is insignificant, truncating the output to
1076 * 8 bytes would still work.
1077 */
1078static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1079{
1080 struct buffer *temp;
1081 struct connection *cli_conn = objt_conn(smp->sess->origin);
1082
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001083 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001084 return 0;
1085
1086 if (!smp_fetch_base32(args, smp, kw, private))
1087 return 0;
1088
1089 temp = get_trash_chunk();
1090 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1091 temp->data += sizeof(unsigned int);
1092
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001093 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001094 case AF_INET:
1095 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001096 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001097 4);
1098 temp->data += 4;
1099 break;
1100 case AF_INET6:
1101 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001102 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001103 16);
1104 temp->data += 16;
1105 break;
1106 default:
1107 return 0;
1108 }
1109
1110 smp->data.u.str = *temp;
1111 smp->data.type = SMP_T_BIN;
1112 return 1;
1113}
1114
1115/* Extracts the query string, which comes after the question mark '?'. If no
1116 * question mark is found, nothing is returned. Otherwise it returns a sample
1117 * of type string carrying the whole query string.
1118 */
1119static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1120{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001121 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001122 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1123 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001124 char *ptr, *end;
1125
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001126 if (!htx)
1127 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001128
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001129 sl = http_get_stline(htx);
1130 ptr = HTX_SL_REQ_UPTR(sl);
1131 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001132
1133 /* look up the '?' */
1134 do {
1135 if (ptr == end)
1136 return 0;
1137 } while (*ptr++ != '?');
1138
1139 smp->data.type = SMP_T_STR;
1140 smp->data.u.str.area = ptr;
1141 smp->data.u.str.data = end - ptr;
1142 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1143 return 1;
1144}
1145
1146static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1147{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001148 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001149 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001150
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001151 if (!htx)
1152 return 0;
1153 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001154 smp->data.u.sint = 1;
1155 return 1;
1156}
1157
1158/* return a valid test if the current request is the first one on the connection */
1159static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1160{
1161 smp->data.type = SMP_T_BOOL;
1162 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1163 return 1;
1164}
1165
1166/* Accepts exactly 1 argument of type userlist */
1167static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1168{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001169 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001170 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001171
1172 if (!args || args->type != ARGT_USR)
1173 return 0;
1174
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001175 if (!htx)
1176 return 0;
1177 if (!get_http_auth(smp, htx))
1178 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001179
1180 smp->data.type = SMP_T_BOOL;
1181 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001182 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001183 return 1;
1184}
1185
1186/* Accepts exactly 1 argument of type userlist */
1187static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1188{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001189 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001190 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001191
Willy Tarreau79e57332018-10-02 16:01:16 +02001192 if (!args || args->type != ARGT_USR)
1193 return 0;
1194
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001195 if (!htx)
1196 return 0;
1197 if (!get_http_auth(smp, htx))
1198 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001199
Willy Tarreau79e57332018-10-02 16:01:16 +02001200 /* if the user does not belong to the userlist or has a wrong password,
1201 * report that it unconditionally does not match. Otherwise we return
1202 * a string containing the username.
1203 */
1204 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1205 smp->strm->txn->auth.pass))
1206 return 0;
1207
1208 /* pat_match_auth() will need the user list */
1209 smp->ctx.a[0] = args->data.usr;
1210
1211 smp->data.type = SMP_T_STR;
1212 smp->flags = SMP_F_CONST;
1213 smp->data.u.str.area = smp->strm->txn->auth.user;
1214 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1215
1216 return 1;
1217}
1218
1219/* Fetch a captured HTTP request header. The index is the position of
1220 * the "capture" option in the configuration file
1221 */
1222static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1223{
1224 struct proxy *fe = strm_fe(smp->strm);
1225 int idx;
1226
1227 if (!args || args->type != ARGT_SINT)
1228 return 0;
1229
1230 idx = args->data.sint;
1231
1232 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1233 return 0;
1234
1235 smp->data.type = SMP_T_STR;
1236 smp->flags |= SMP_F_CONST;
1237 smp->data.u.str.area = smp->strm->req_cap[idx];
1238 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1239
1240 return 1;
1241}
1242
1243/* Fetch a captured HTTP response header. The index is the position of
1244 * the "capture" option in the configuration file
1245 */
1246static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1247{
1248 struct proxy *fe = strm_fe(smp->strm);
1249 int idx;
1250
1251 if (!args || args->type != ARGT_SINT)
1252 return 0;
1253
1254 idx = args->data.sint;
1255
1256 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1257 return 0;
1258
1259 smp->data.type = SMP_T_STR;
1260 smp->flags |= SMP_F_CONST;
1261 smp->data.u.str.area = smp->strm->res_cap[idx];
1262 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1263
1264 return 1;
1265}
1266
1267/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1268static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1269{
1270 struct buffer *temp;
1271 struct http_txn *txn = smp->strm->txn;
1272 char *ptr;
1273
1274 if (!txn || !txn->uri)
1275 return 0;
1276
1277 ptr = txn->uri;
1278
1279 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1280 ptr++;
1281
1282 temp = get_trash_chunk();
1283 temp->area = txn->uri;
1284 temp->data = ptr - txn->uri;
1285 smp->data.u.str = *temp;
1286 smp->data.type = SMP_T_STR;
1287 smp->flags = SMP_F_CONST;
1288
1289 return 1;
1290
1291}
1292
1293/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1294static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1295{
1296 struct http_txn *txn = smp->strm->txn;
1297 struct ist path;
1298 const char *ptr;
1299
1300 if (!txn || !txn->uri)
1301 return 0;
1302
1303 ptr = txn->uri;
1304
1305 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1306 ptr++;
1307
1308 if (!*ptr)
1309 return 0;
1310
Christopher Faulet78337bb2018-11-15 14:35:18 +01001311 /* skip the first space and find space after URI */
1312 path = ist2(++ptr, 0);
1313 while (*ptr != ' ' && *ptr != '\0')
1314 ptr++;
1315 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001316
Christopher Faulet78337bb2018-11-15 14:35:18 +01001317 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02001318 if (!path.ptr)
1319 return 0;
1320
1321 smp->data.u.str.area = path.ptr;
1322 smp->data.u.str.data = path.len;
1323 smp->data.type = SMP_T_STR;
1324 smp->flags = SMP_F_CONST;
1325
1326 return 1;
1327}
1328
1329/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1330 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1331 */
1332static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1333{
1334 struct http_txn *txn = smp->strm->txn;
1335
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001336 if (!txn || txn->req.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001337 return 0;
1338
1339 if (txn->req.flags & HTTP_MSGF_VER_11)
1340 smp->data.u.str.area = "HTTP/1.1";
1341 else
1342 smp->data.u.str.area = "HTTP/1.0";
1343
1344 smp->data.u.str.data = 8;
1345 smp->data.type = SMP_T_STR;
1346 smp->flags = SMP_F_CONST;
1347 return 1;
1348
1349}
1350
1351/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1352 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1353 */
1354static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1355{
1356 struct http_txn *txn = smp->strm->txn;
1357
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001358 if (!txn || txn->rsp.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001359 return 0;
1360
1361 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1362 smp->data.u.str.area = "HTTP/1.1";
1363 else
1364 smp->data.u.str.area = "HTTP/1.0";
1365
1366 smp->data.u.str.data = 8;
1367 smp->data.type = SMP_T_STR;
1368 smp->flags = SMP_F_CONST;
1369 return 1;
1370
1371}
1372
1373/* Iterate over all cookies present in a message. The context is stored in
1374 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1375 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1376 * the direction, multiple cookies may be parsed on the same line or not.
1377 * The cookie name is in args and the name length in args->data.str.len.
1378 * Accepts exactly 1 argument of type string. If the input options indicate
1379 * that no iterating is desired, then only last value is fetched if any.
1380 * The returned sample is of type CSTR. Can be used to parse cookies in other
1381 * files.
1382 */
1383static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1384{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001385 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1386 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001387 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1388 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1389 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001390 int occ = 0;
1391 int found = 0;
1392
1393 if (!args || args->type != ARGT_STR)
1394 return 0;
1395
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001396 if (!ctx) {
1397 /* first call */
1398 ctx = &static_http_hdr_ctx;
1399 ctx->blk = NULL;
1400 smp->ctx.a[2] = ctx;
1401 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001402
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001403 if (!htx)
1404 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001405
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001406 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001407
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001408 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1409 /* no explicit occurrence and single fetch => last cookie by default */
1410 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001411
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001412 /* OK so basically here, either we want only one value and it's the
1413 * last one, or we want to iterate over all of them and we fetch the
1414 * next one.
1415 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001416
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001417 if (!(smp->flags & SMP_F_NOT_LAST)) {
1418 /* search for the header from the beginning, we must first initialize
1419 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001420 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001421 smp->ctx.a[0] = NULL;
1422 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001423 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001424
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001425 smp->flags |= SMP_F_VOL_HDR;
1426 while (1) {
1427 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1428 if (!smp->ctx.a[0]) {
1429 if (!http_find_header(htx, hdr, ctx, 0))
1430 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001431
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001432 if (ctx->value.len < args->data.str.data + 1)
1433 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001434
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001435 smp->ctx.a[0] = ctx->value.ptr;
1436 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001437 }
1438
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001439 smp->data.type = SMP_T_STR;
1440 smp->flags |= SMP_F_CONST;
1441 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
1442 args->data.str.area, args->data.str.data,
1443 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1444 &smp->data.u.str.area,
1445 &smp->data.u.str.data);
1446 if (smp->ctx.a[0]) {
1447 found = 1;
1448 if (occ >= 0) {
1449 /* one value was returned into smp->data.u.str.{str,len} */
1450 smp->flags |= SMP_F_NOT_LAST;
1451 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001452 }
1453 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001454 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001455 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001456
Willy Tarreau79e57332018-10-02 16:01:16 +02001457 /* all cookie headers and values were scanned. If we're looking for the
1458 * last occurrence, we may return it now.
1459 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001460 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001461 smp->flags &= ~SMP_F_NOT_LAST;
1462 return found;
1463}
1464
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001465/* Same than smp_fetch_cookie() but only relies on the sample direction to
1466 * choose the right channel. So instead of duplicating the code, we just change
1467 * the keyword and then fallback on smp_fetch_cookie().
1468 */
1469static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1470{
1471 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1472 return smp_fetch_cookie(args, smp, kw, private);
1473}
1474
Willy Tarreau79e57332018-10-02 16:01:16 +02001475/* Iterate over all cookies present in a request to count how many occurrences
1476 * match the name in args and args->data.str.len. If <multi> is non-null, then
1477 * multiple cookies may be parsed on the same line. The returned sample is of
1478 * type UINT. Accepts exactly 1 argument of type string.
1479 */
1480static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1481{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001482 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1483 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001484 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1485 struct http_hdr_ctx ctx;
1486 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001487 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001488 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001489
1490 if (!args || args->type != ARGT_STR)
1491 return 0;
1492
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001493 if (!htx)
1494 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001495
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001496 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001497
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001498 val_end = val_beg = NULL;
1499 ctx.blk = NULL;
1500 cnt = 0;
1501 while (1) {
1502 /* Note: val_beg == NULL every time we need to fetch a new header */
1503 if (!val_beg) {
1504 if (!http_find_header(htx, hdr, &ctx, 0))
1505 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001506
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001507 if (ctx.value.len < args->data.str.data + 1)
1508 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001509
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001510 val_beg = ctx.value.ptr;
1511 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001512 }
1513
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001514 smp->data.type = SMP_T_STR;
1515 smp->flags |= SMP_F_CONST;
1516 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
1517 args->data.str.area, args->data.str.data,
1518 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1519 &smp->data.u.str.area,
1520 &smp->data.u.str.data))) {
1521 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001522 }
1523 }
1524
1525 smp->data.type = SMP_T_SINT;
1526 smp->data.u.sint = cnt;
1527 smp->flags |= SMP_F_VOL_HDR;
1528 return 1;
1529}
1530
1531/* Fetch an cookie's integer value. The integer value is returned. It
1532 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1533 */
1534static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1535{
1536 int ret = smp_fetch_cookie(args, smp, kw, private);
1537
1538 if (ret > 0) {
1539 smp->data.type = SMP_T_SINT;
1540 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1541 smp->data.u.str.data);
1542 }
1543
1544 return ret;
1545}
1546
1547/************************************************************************/
1548/* The code below is dedicated to sample fetches */
1549/************************************************************************/
1550
1551/* This scans a URL-encoded query string. It takes an optionally wrapping
1552 * string whose first contigous chunk has its beginning in ctx->a[0] and end
1553 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1554 * pointers are updated for next iteration before leaving.
1555 */
1556static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1557{
1558 const char *vstart, *vend;
1559 struct buffer *temp;
1560 const char **chunks = (const char **)smp->ctx.a;
1561
1562 if (!http_find_next_url_param(chunks, name, name_len,
1563 &vstart, &vend, delim))
1564 return 0;
1565
1566 /* Create sample. If the value is contiguous, return the pointer as CONST,
1567 * if the value is wrapped, copy-it in a buffer.
1568 */
1569 smp->data.type = SMP_T_STR;
1570 if (chunks[2] &&
1571 vstart >= chunks[0] && vstart <= chunks[1] &&
1572 vend >= chunks[2] && vend <= chunks[3]) {
1573 /* Wrapped case. */
1574 temp = get_trash_chunk();
1575 memcpy(temp->area, vstart, chunks[1] - vstart);
1576 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1577 vend - chunks[2]);
1578 smp->data.u.str.area = temp->area;
1579 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1580 } else {
1581 /* Contiguous case. */
1582 smp->data.u.str.area = (char *)vstart;
1583 smp->data.u.str.data = vend - vstart;
1584 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1585 }
1586
1587 /* Update context, check wrapping. */
1588 chunks[0] = vend;
1589 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1590 chunks[1] = chunks[3];
1591 chunks[2] = NULL;
1592 }
1593
1594 if (chunks[0] < chunks[1])
1595 smp->flags |= SMP_F_NOT_LAST;
1596
1597 return 1;
1598}
1599
1600/* This function iterates over each parameter of the query string. It uses
1601 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1602 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1603 * An optional parameter name is passed in args[0], otherwise any parameter is
1604 * considered. It supports an optional delimiter argument for the beginning of
1605 * the string in args[1], which defaults to "?".
1606 */
1607static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1608{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001609 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001610 char delim = '?';
1611 const char *name;
1612 int name_len;
1613
1614 if (!args ||
1615 (args[0].type && args[0].type != ARGT_STR) ||
1616 (args[1].type && args[1].type != ARGT_STR))
1617 return 0;
1618
1619 name = "";
1620 name_len = 0;
1621 if (args->type == ARGT_STR) {
1622 name = args->data.str.area;
1623 name_len = args->data.str.data;
1624 }
1625
1626 if (args[1].type)
1627 delim = *args[1].data.str.area;
1628
1629 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001630 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1631 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001632
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001633 if (!htx)
1634 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001635
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001636 sl = http_get_stline(htx);
1637 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1638 if (!smp->ctx.a[0])
1639 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001640
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001641 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001642
1643 /* Assume that the context is filled with NULL pointer
1644 * before the first call.
1645 * smp->ctx.a[2] = NULL;
1646 * smp->ctx.a[3] = NULL;
1647 */
1648 }
1649
1650 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1651}
1652
1653/* This function iterates over each parameter of the body. This requires
1654 * that the body has been waited for using http-buffer-request. It uses
1655 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
1656 * contigous part of the body, and optionally ctx->a[2..3] to reference the
1657 * optional second part if the body wraps at the end of the buffer. An optional
1658 * parameter name is passed in args[0], otherwise any parameter is considered.
1659 */
1660static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1661{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001662 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001663 const char *name;
1664 int name_len;
1665
1666 if (!args || (args[0].type && args[0].type != ARGT_STR))
1667 return 0;
1668
1669 name = "";
1670 name_len = 0;
1671 if (args[0].type == ARGT_STR) {
1672 name = args[0].data.str.area;
1673 name_len = args[0].data.str.data;
1674 }
1675
1676 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001677 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1678 struct buffer *temp;
1679 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001680
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001681 if (!htx)
1682 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001683
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001684 temp = get_trash_chunk();
1685 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1686 struct htx_blk *blk = htx_get_blk(htx, pos);
1687 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001688
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001689 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1690 break;
1691 if (type == HTX_BLK_DATA) {
1692 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
1693 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001694 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001695 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001696
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001697 smp->ctx.a[0] = temp->area;
1698 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001699
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001700 /* Assume that the context is filled with NULL pointer
1701 * before the first call.
1702 * smp->ctx.a[2] = NULL;
1703 * smp->ctx.a[3] = NULL;
1704 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001705
Willy Tarreau79e57332018-10-02 16:01:16 +02001706 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001707
Willy Tarreau79e57332018-10-02 16:01:16 +02001708 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1709}
1710
1711/* Return the signed integer value for the specified url parameter (see url_param
1712 * above).
1713 */
1714static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1715{
1716 int ret = smp_fetch_url_param(args, smp, kw, private);
1717
1718 if (ret > 0) {
1719 smp->data.type = SMP_T_SINT;
1720 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1721 smp->data.u.str.data);
1722 }
1723
1724 return ret;
1725}
1726
1727/* This produces a 32-bit hash of the concatenation of the first occurrence of
1728 * the Host header followed by the path component if it begins with a slash ('/').
1729 * This means that '*' will not be added, resulting in exactly the first Host
1730 * entry. If no Host header is found, then the path is used. The resulting value
1731 * is hashed using the url hash followed by a full avalanche hash and provides a
1732 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1733 * high-traffic sites without having to store whole paths.
1734 * this differs from the base32 functions in that it includes the url parameters
1735 * as well as the path
1736 */
1737static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1738{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001739 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001740 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1741 struct http_hdr_ctx ctx;
1742 struct htx_sl *sl;
1743 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001744 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001745
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001746 if (!htx)
1747 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001748
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001749 ctx.blk = NULL;
1750 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1751 /* OK we have the header value in ctx.value */
1752 while (ctx.value.len--)
1753 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001754 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001755
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001756 /* now retrieve the path */
1757 sl = http_get_stline(htx);
1758 path = http_get_path(htx_sl_req_uri(sl));
1759 while (path.len > 0 && *(path.ptr) != '?') {
1760 path.ptr++;
1761 path.len--;
1762 }
1763 if (path.len && *(path.ptr) == '/') {
1764 while (path.len--)
1765 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001766 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001767
Willy Tarreau79e57332018-10-02 16:01:16 +02001768 hash = full_hash(hash);
1769
1770 smp->data.type = SMP_T_SINT;
1771 smp->data.u.sint = hash;
1772 smp->flags = SMP_F_VOL_1ST;
1773 return 1;
1774}
1775
1776/* This concatenates the source address with the 32-bit hash of the Host and
1777 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1778 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1779 * on the source address length. The URL hash is stored before the address so
1780 * that in environments where IPv6 is insignificant, truncating the output to
1781 * 8 bytes would still work.
1782 */
1783static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1784{
1785 struct buffer *temp;
1786 struct connection *cli_conn = objt_conn(smp->sess->origin);
1787
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001788 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001789 return 0;
1790
1791 if (!smp_fetch_url32(args, smp, kw, private))
1792 return 0;
1793
1794 temp = get_trash_chunk();
1795 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1796 temp->data += sizeof(unsigned int);
1797
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001798 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001799 case AF_INET:
1800 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001801 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001802 4);
1803 temp->data += 4;
1804 break;
1805 case AF_INET6:
1806 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001807 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001808 16);
1809 temp->data += 16;
1810 break;
1811 default:
1812 return 0;
1813 }
1814
1815 smp->data.u.str = *temp;
1816 smp->data.type = SMP_T_BIN;
1817 return 1;
1818}
1819
1820/************************************************************************/
1821/* Other utility functions */
1822/************************************************************************/
1823
1824/* This function is used to validate the arguments passed to any "hdr" fetch
1825 * keyword. These keywords support an optional positive or negative occurrence
1826 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
1827 * is assumed that the types are already the correct ones. Returns 0 on error,
1828 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
1829 * error message in case of error, that the caller is responsible for freeing.
1830 * The initial location must either be freeable or NULL.
1831 * Note: this function's pointer is checked from Lua.
1832 */
1833int val_hdr(struct arg *arg, char **err_msg)
1834{
1835 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
1836 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
1837 return 0;
1838 }
1839 return 1;
1840}
1841
1842/************************************************************************/
1843/* All supported sample fetch keywords must be declared here. */
1844/************************************************************************/
1845
1846/* Note: must not be declared <const> as its list will be overwritten */
1847static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
1848 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1849 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
1850 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
1851
1852 /* capture are allocated and are permanent in the stream */
1853 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
1854
1855 /* retrieve these captures from the HTTP logs */
1856 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1857 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1858 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1859
1860 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
1861 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1862
1863 /* cookie is valid in both directions (eg: for "stick ...") but cook*
1864 * are only here to match the ACL's name, are request-only and are used
1865 * for ACL compatibility only.
1866 */
1867 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001868 { "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 +02001869 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1870 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1871
1872 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
1873 * only here to match the ACL's name, are request-only and are used for
1874 * ACL compatibility only.
1875 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001876 { "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 +02001877 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1878 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
1879 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
1880
1881 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
1882 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
1883 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
1884 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
1885 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1886 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1887
1888 /* HTTP protocol on the request path */
1889 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
1890 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
1891
1892 /* HTTP version on the request path */
1893 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1894 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1895
1896 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
1897 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
1898 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
1899 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
1900
1901 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
1902 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
1903
1904 /* HTTP version on the response path */
1905 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
1906 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
1907
1908 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
1909 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
1910 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1911 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1912
1913 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
1914 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1915 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
1916 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1917 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
1918 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
1919 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
1920
1921 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
1922 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
1923 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1924 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1925
1926 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
1927 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1928 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
1929 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1930 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
1931 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
1932 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
1933
1934 /* scook is valid only on the response and is used for ACL compatibility */
1935 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
1936 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1937 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1938 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
1939
1940 /* shdr is valid only on the response and is used for ACL compatibility */
1941 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
1942 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1943 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
1944 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
1945
1946 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
1947 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
1948 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1949 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
1950 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
1951 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
1952 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
1953 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
1954 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
1955 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1956 { /* END */ },
1957}};
1958
Willy Tarreau0108d902018-11-25 19:14:37 +01001959INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02001960
1961/*
1962 * Local variables:
1963 * c-indent-level: 8
1964 * c-basic-offset: 8
1965 * End:
1966 */