blob: d288e841dad3dfdbf152eaa6cb43041254c9c9b0 [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';
Tim Duesterhus530408f2020-02-26 16:20:49 +0100419 build_logline(smp->strm, smp->strm->unique_id,
420 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
Willy Tarreau79e57332018-10-02 16:01:16 +0200421 }
Tim Duesterhus530408f2020-02-26 16:20:49 +0100422 smp->data.u.str.data = strlen(smp->strm->unique_id);
Willy Tarreau79e57332018-10-02 16:01:16 +0200423 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;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200985
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200986 if (!htx)
987 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200988
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200989 sl = http_get_stline(htx);
Jerome Magnin4fb196c2020-02-21 10:49:12 +0100990 path = iststop(http_get_path(htx_sl_req_uri(sl)), '?');
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200991 if (!path.ptr)
992 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200993
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200994 /* OK, we got the '/' ! */
995 smp->data.type = SMP_T_STR;
996 smp->data.u.str.area = path.ptr;
Jerome Magnin4fb196c2020-02-21 10:49:12 +0100997 smp->data.u.str.data = path.len;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200998 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200999 return 1;
1000}
1001
1002/* This produces a concatenation of the first occurrence of the Host header
1003 * followed by the path component if it begins with a slash ('/'). This means
1004 * that '*' will not be added, resulting in exactly the first Host entry.
1005 * If no Host header is found, then the path is returned as-is. The returned
1006 * value is stored in the trash so it does not need to be marked constant.
1007 * The returned sample is of type string.
1008 */
1009static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1010{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001011 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001012 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1013 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001014 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001015 struct http_hdr_ctx ctx;
1016 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001017
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001018 if (!htx)
1019 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001020
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001021 ctx.blk = NULL;
1022 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1023 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001024
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001025 /* OK we have the header value in ctx.value */
1026 temp = get_trash_chunk();
1027 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001028
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001029 /* now retrieve the path */
1030 sl = http_get_stline(htx);
1031 path = http_get_path(htx_sl_req_uri(sl));
1032 if (path.ptr) {
1033 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001034
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001035 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1036 ;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001037
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001038 if (len && *(path.ptr) == '/')
1039 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001040 }
1041
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001042 smp->data.type = SMP_T_STR;
1043 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001044 smp->flags = SMP_F_VOL_1ST;
1045 return 1;
1046}
1047
1048/* This produces a 32-bit hash of the concatenation of the first occurrence of
1049 * the Host header followed by the path component if it begins with a slash ('/').
1050 * This means that '*' will not be added, resulting in exactly the first Host
1051 * entry. If no Host header is found, then the path is used. The resulting value
1052 * is hashed using the path hash followed by a full avalanche hash and provides a
1053 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1054 * high-traffic sites without having to store whole paths.
1055 */
1056static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1057{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001058 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001059 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1060 struct htx_sl *sl;
1061 struct http_hdr_ctx ctx;
1062 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001063 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001064
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001065 if (!htx)
1066 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001067
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001068 ctx.blk = NULL;
1069 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1070 /* OK we have the header value in ctx.value */
1071 while (ctx.value.len--)
1072 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001073 }
1074
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001075 /* now retrieve the path */
1076 sl = http_get_stline(htx);
1077 path = http_get_path(htx_sl_req_uri(sl));
1078 if (path.ptr) {
1079 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001080
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001081 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1082 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001083
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001084 if (len && *(path.ptr) == '/') {
1085 while (len--)
1086 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001087 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001088 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001089
Willy Tarreau79e57332018-10-02 16:01:16 +02001090 hash = full_hash(hash);
1091
1092 smp->data.type = SMP_T_SINT;
1093 smp->data.u.sint = hash;
1094 smp->flags = SMP_F_VOL_1ST;
1095 return 1;
1096}
1097
1098/* This concatenates the source address with the 32-bit hash of the Host and
1099 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1100 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1101 * on the source address length. The path hash is stored before the address so
1102 * that in environments where IPv6 is insignificant, truncating the output to
1103 * 8 bytes would still work.
1104 */
1105static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1106{
1107 struct buffer *temp;
1108 struct connection *cli_conn = objt_conn(smp->sess->origin);
1109
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001110 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001111 return 0;
1112
1113 if (!smp_fetch_base32(args, smp, kw, private))
1114 return 0;
1115
1116 temp = get_trash_chunk();
1117 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1118 temp->data += sizeof(unsigned int);
1119
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001120 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001121 case AF_INET:
1122 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001123 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001124 4);
1125 temp->data += 4;
1126 break;
1127 case AF_INET6:
1128 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001129 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001130 16);
1131 temp->data += 16;
1132 break;
1133 default:
1134 return 0;
1135 }
1136
1137 smp->data.u.str = *temp;
1138 smp->data.type = SMP_T_BIN;
1139 return 1;
1140}
1141
1142/* Extracts the query string, which comes after the question mark '?'. If no
1143 * question mark is found, nothing is returned. Otherwise it returns a sample
1144 * of type string carrying the whole query string.
1145 */
1146static int smp_fetch_query(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, 1);
1150 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001151 char *ptr, *end;
1152
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001153 if (!htx)
1154 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001155
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001156 sl = http_get_stline(htx);
1157 ptr = HTX_SL_REQ_UPTR(sl);
1158 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001159
1160 /* look up the '?' */
1161 do {
1162 if (ptr == end)
1163 return 0;
1164 } while (*ptr++ != '?');
1165
1166 smp->data.type = SMP_T_STR;
1167 smp->data.u.str.area = ptr;
1168 smp->data.u.str.data = end - ptr;
1169 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1170 return 1;
1171}
1172
1173static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1174{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001175 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001176 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001177
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001178 if (!htx)
1179 return 0;
1180 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001181 smp->data.u.sint = 1;
1182 return 1;
1183}
1184
1185/* return a valid test if the current request is the first one on the connection */
1186static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1187{
1188 smp->data.type = SMP_T_BOOL;
1189 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1190 return 1;
1191}
1192
Christopher Fauleta4063562019-08-02 11:51:37 +02001193/* Fetch the authentication method if there is an Authorization header. It
1194 * relies on get_http_auth()
1195 */
1196static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1197{
1198 struct channel *chn = SMP_REQ_CHN(smp);
1199 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1200 struct http_txn *txn;
1201
1202 if (!htx)
1203 return 0;
1204
1205 txn = smp->strm->txn;
1206 if (!get_http_auth(smp, htx))
1207 return 0;
1208
1209 switch (txn->auth.method) {
1210 case HTTP_AUTH_BASIC:
1211 smp->data.u.str.area = "Basic";
1212 smp->data.u.str.data = 5;
1213 break;
1214 case HTTP_AUTH_DIGEST:
1215 /* Unexpected because not supported */
1216 smp->data.u.str.area = "Digest";
1217 smp->data.u.str.data = 6;
1218 break;
1219 default:
1220 return 0;
1221 }
1222
1223 smp->data.type = SMP_T_STR;
1224 smp->flags = SMP_F_CONST;
1225 return 1;
1226}
1227
1228/* Fetch the user supplied if there is an Authorization header. It relies on
1229 * get_http_auth()
1230 */
1231static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1232{
1233 struct channel *chn = SMP_REQ_CHN(smp);
1234 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1235 struct http_txn *txn;
1236
1237 if (!htx)
1238 return 0;
1239
1240 txn = smp->strm->txn;
1241 if (!get_http_auth(smp, htx))
1242 return 0;
1243
1244 smp->data.type = SMP_T_STR;
1245 smp->data.u.str.area = txn->auth.user;
1246 smp->data.u.str.data = strlen(txn->auth.user);
1247 smp->flags = SMP_F_CONST;
1248 return 1;
1249}
1250
1251/* Fetch the password supplied if there is an Authorization header. It relies on
1252 * get_http_auth()
1253 */
1254static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1255{
1256 struct channel *chn = SMP_REQ_CHN(smp);
1257 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1258 struct http_txn *txn;
1259
1260 if (!htx)
1261 return 0;
1262
1263 txn = smp->strm->txn;
1264 if (!get_http_auth(smp, htx))
1265 return 0;
1266
1267 smp->data.type = SMP_T_STR;
1268 smp->data.u.str.area = txn->auth.pass;
1269 smp->data.u.str.data = strlen(txn->auth.pass);
1270 smp->flags = SMP_F_CONST;
1271 return 1;
1272}
1273
Willy Tarreau79e57332018-10-02 16:01:16 +02001274/* Accepts exactly 1 argument of type userlist */
1275static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1276{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001277 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001278 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001279
1280 if (!args || args->type != ARGT_USR)
1281 return 0;
1282
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001283 if (!htx)
1284 return 0;
1285 if (!get_http_auth(smp, htx))
1286 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001287
1288 smp->data.type = SMP_T_BOOL;
1289 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001290 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001291 return 1;
1292}
1293
1294/* Accepts exactly 1 argument of type userlist */
1295static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1296{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001297 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001298 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001299
Willy Tarreau79e57332018-10-02 16:01:16 +02001300 if (!args || args->type != ARGT_USR)
1301 return 0;
1302
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001303 if (!htx)
1304 return 0;
1305 if (!get_http_auth(smp, htx))
1306 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001307
Willy Tarreau79e57332018-10-02 16:01:16 +02001308 /* if the user does not belong to the userlist or has a wrong password,
1309 * report that it unconditionally does not match. Otherwise we return
1310 * a string containing the username.
1311 */
1312 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1313 smp->strm->txn->auth.pass))
1314 return 0;
1315
1316 /* pat_match_auth() will need the user list */
1317 smp->ctx.a[0] = args->data.usr;
1318
1319 smp->data.type = SMP_T_STR;
1320 smp->flags = SMP_F_CONST;
1321 smp->data.u.str.area = smp->strm->txn->auth.user;
1322 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1323
1324 return 1;
1325}
1326
1327/* Fetch a captured HTTP request header. The index is the position of
1328 * the "capture" option in the configuration file
1329 */
1330static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1331{
1332 struct proxy *fe = strm_fe(smp->strm);
1333 int idx;
1334
1335 if (!args || args->type != ARGT_SINT)
1336 return 0;
1337
1338 idx = args->data.sint;
1339
1340 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1341 return 0;
1342
1343 smp->data.type = SMP_T_STR;
1344 smp->flags |= SMP_F_CONST;
1345 smp->data.u.str.area = smp->strm->req_cap[idx];
1346 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1347
1348 return 1;
1349}
1350
1351/* Fetch a captured HTTP response header. The index is the position of
1352 * the "capture" option in the configuration file
1353 */
1354static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1355{
1356 struct proxy *fe = strm_fe(smp->strm);
1357 int idx;
1358
1359 if (!args || args->type != ARGT_SINT)
1360 return 0;
1361
1362 idx = args->data.sint;
1363
1364 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1365 return 0;
1366
1367 smp->data.type = SMP_T_STR;
1368 smp->flags |= SMP_F_CONST;
1369 smp->data.u.str.area = smp->strm->res_cap[idx];
1370 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1371
1372 return 1;
1373}
1374
1375/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1376static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1377{
1378 struct buffer *temp;
1379 struct http_txn *txn = smp->strm->txn;
1380 char *ptr;
1381
1382 if (!txn || !txn->uri)
1383 return 0;
1384
1385 ptr = txn->uri;
1386
1387 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1388 ptr++;
1389
1390 temp = get_trash_chunk();
1391 temp->area = txn->uri;
1392 temp->data = ptr - txn->uri;
1393 smp->data.u.str = *temp;
1394 smp->data.type = SMP_T_STR;
1395 smp->flags = SMP_F_CONST;
1396
1397 return 1;
1398
1399}
1400
1401/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1402static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1403{
1404 struct http_txn *txn = smp->strm->txn;
1405 struct ist path;
1406 const char *ptr;
1407
1408 if (!txn || !txn->uri)
1409 return 0;
1410
1411 ptr = txn->uri;
1412
1413 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1414 ptr++;
1415
1416 if (!*ptr)
1417 return 0;
1418
Christopher Faulet78337bb2018-11-15 14:35:18 +01001419 /* skip the first space and find space after URI */
1420 path = ist2(++ptr, 0);
1421 while (*ptr != ' ' && *ptr != '\0')
1422 ptr++;
1423 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001424
Christopher Faulet78337bb2018-11-15 14:35:18 +01001425 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02001426 if (!path.ptr)
1427 return 0;
1428
1429 smp->data.u.str.area = path.ptr;
1430 smp->data.u.str.data = path.len;
1431 smp->data.type = SMP_T_STR;
1432 smp->flags = SMP_F_CONST;
1433
1434 return 1;
1435}
1436
1437/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1438 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1439 */
1440static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1441{
1442 struct http_txn *txn = smp->strm->txn;
1443
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001444 if (!txn || txn->req.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001445 return 0;
1446
1447 if (txn->req.flags & HTTP_MSGF_VER_11)
1448 smp->data.u.str.area = "HTTP/1.1";
1449 else
1450 smp->data.u.str.area = "HTTP/1.0";
1451
1452 smp->data.u.str.data = 8;
1453 smp->data.type = SMP_T_STR;
1454 smp->flags = SMP_F_CONST;
1455 return 1;
1456
1457}
1458
1459/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1460 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1461 */
1462static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1463{
1464 struct http_txn *txn = smp->strm->txn;
1465
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001466 if (!txn || txn->rsp.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001467 return 0;
1468
1469 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1470 smp->data.u.str.area = "HTTP/1.1";
1471 else
1472 smp->data.u.str.area = "HTTP/1.0";
1473
1474 smp->data.u.str.data = 8;
1475 smp->data.type = SMP_T_STR;
1476 smp->flags = SMP_F_CONST;
1477 return 1;
1478
1479}
1480
1481/* Iterate over all cookies present in a message. The context is stored in
1482 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1483 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1484 * the direction, multiple cookies may be parsed on the same line or not.
1485 * The cookie name is in args and the name length in args->data.str.len.
1486 * Accepts exactly 1 argument of type string. If the input options indicate
1487 * that no iterating is desired, then only last value is fetched if any.
1488 * The returned sample is of type CSTR. Can be used to parse cookies in other
1489 * files.
1490 */
1491static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1492{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001493 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1494 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001495 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1496 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1497 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001498 int occ = 0;
1499 int found = 0;
1500
1501 if (!args || args->type != ARGT_STR)
1502 return 0;
1503
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001504 if (!ctx) {
1505 /* first call */
1506 ctx = &static_http_hdr_ctx;
1507 ctx->blk = NULL;
1508 smp->ctx.a[2] = ctx;
1509 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001510
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001511 if (!htx)
1512 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001513
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001514 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001515
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001516 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1517 /* no explicit occurrence and single fetch => last cookie by default */
1518 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001519
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001520 /* OK so basically here, either we want only one value and it's the
1521 * last one, or we want to iterate over all of them and we fetch the
1522 * next one.
1523 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001524
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001525 if (!(smp->flags & SMP_F_NOT_LAST)) {
1526 /* search for the header from the beginning, we must first initialize
1527 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001528 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001529 smp->ctx.a[0] = NULL;
1530 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001531 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001532
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001533 smp->flags |= SMP_F_VOL_HDR;
1534 while (1) {
1535 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1536 if (!smp->ctx.a[0]) {
1537 if (!http_find_header(htx, hdr, ctx, 0))
1538 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001539
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001540 if (ctx->value.len < args->data.str.data + 1)
1541 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001542
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001543 smp->ctx.a[0] = ctx->value.ptr;
1544 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001545 }
1546
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001547 smp->data.type = SMP_T_STR;
1548 smp->flags |= SMP_F_CONST;
1549 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
1550 args->data.str.area, args->data.str.data,
1551 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1552 &smp->data.u.str.area,
1553 &smp->data.u.str.data);
1554 if (smp->ctx.a[0]) {
1555 found = 1;
1556 if (occ >= 0) {
1557 /* one value was returned into smp->data.u.str.{str,len} */
1558 smp->flags |= SMP_F_NOT_LAST;
1559 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001560 }
1561 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001562 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001563 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001564
Willy Tarreau79e57332018-10-02 16:01:16 +02001565 /* all cookie headers and values were scanned. If we're looking for the
1566 * last occurrence, we may return it now.
1567 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001568 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001569 smp->flags &= ~SMP_F_NOT_LAST;
1570 return found;
1571}
1572
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001573/* Same than smp_fetch_cookie() but only relies on the sample direction to
1574 * choose the right channel. So instead of duplicating the code, we just change
1575 * the keyword and then fallback on smp_fetch_cookie().
1576 */
1577static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1578{
1579 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1580 return smp_fetch_cookie(args, smp, kw, private);
1581}
1582
Willy Tarreau79e57332018-10-02 16:01:16 +02001583/* Iterate over all cookies present in a request to count how many occurrences
1584 * match the name in args and args->data.str.len. If <multi> is non-null, then
1585 * multiple cookies may be parsed on the same line. The returned sample is of
1586 * type UINT. Accepts exactly 1 argument of type string.
1587 */
1588static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1589{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001590 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1591 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001592 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1593 struct http_hdr_ctx ctx;
1594 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001595 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001596 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001597
1598 if (!args || args->type != ARGT_STR)
1599 return 0;
1600
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001601 if (!htx)
1602 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001603
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001604 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001605
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001606 val_end = val_beg = NULL;
1607 ctx.blk = NULL;
1608 cnt = 0;
1609 while (1) {
1610 /* Note: val_beg == NULL every time we need to fetch a new header */
1611 if (!val_beg) {
1612 if (!http_find_header(htx, hdr, &ctx, 0))
1613 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001614
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001615 if (ctx.value.len < args->data.str.data + 1)
1616 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001617
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001618 val_beg = ctx.value.ptr;
1619 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001620 }
1621
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001622 smp->data.type = SMP_T_STR;
1623 smp->flags |= SMP_F_CONST;
1624 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
1625 args->data.str.area, args->data.str.data,
1626 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1627 &smp->data.u.str.area,
1628 &smp->data.u.str.data))) {
1629 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001630 }
1631 }
1632
1633 smp->data.type = SMP_T_SINT;
1634 smp->data.u.sint = cnt;
1635 smp->flags |= SMP_F_VOL_HDR;
1636 return 1;
1637}
1638
1639/* Fetch an cookie's integer value. The integer value is returned. It
1640 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1641 */
1642static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1643{
1644 int ret = smp_fetch_cookie(args, smp, kw, private);
1645
1646 if (ret > 0) {
1647 smp->data.type = SMP_T_SINT;
1648 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1649 smp->data.u.str.data);
1650 }
1651
1652 return ret;
1653}
1654
1655/************************************************************************/
1656/* The code below is dedicated to sample fetches */
1657/************************************************************************/
1658
1659/* This scans a URL-encoded query string. It takes an optionally wrapping
1660 * string whose first contigous chunk has its beginning in ctx->a[0] and end
1661 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1662 * pointers are updated for next iteration before leaving.
1663 */
1664static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1665{
1666 const char *vstart, *vend;
1667 struct buffer *temp;
1668 const char **chunks = (const char **)smp->ctx.a;
1669
1670 if (!http_find_next_url_param(chunks, name, name_len,
1671 &vstart, &vend, delim))
1672 return 0;
1673
1674 /* Create sample. If the value is contiguous, return the pointer as CONST,
1675 * if the value is wrapped, copy-it in a buffer.
1676 */
1677 smp->data.type = SMP_T_STR;
1678 if (chunks[2] &&
1679 vstart >= chunks[0] && vstart <= chunks[1] &&
1680 vend >= chunks[2] && vend <= chunks[3]) {
1681 /* Wrapped case. */
1682 temp = get_trash_chunk();
1683 memcpy(temp->area, vstart, chunks[1] - vstart);
1684 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1685 vend - chunks[2]);
1686 smp->data.u.str.area = temp->area;
1687 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1688 } else {
1689 /* Contiguous case. */
1690 smp->data.u.str.area = (char *)vstart;
1691 smp->data.u.str.data = vend - vstart;
1692 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1693 }
1694
1695 /* Update context, check wrapping. */
1696 chunks[0] = vend;
1697 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1698 chunks[1] = chunks[3];
1699 chunks[2] = NULL;
1700 }
1701
1702 if (chunks[0] < chunks[1])
1703 smp->flags |= SMP_F_NOT_LAST;
1704
1705 return 1;
1706}
1707
1708/* This function iterates over each parameter of the query string. It uses
1709 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1710 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1711 * An optional parameter name is passed in args[0], otherwise any parameter is
1712 * considered. It supports an optional delimiter argument for the beginning of
1713 * the string in args[1], which defaults to "?".
1714 */
1715static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1716{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001717 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001718 char delim = '?';
1719 const char *name;
1720 int name_len;
1721
1722 if (!args ||
1723 (args[0].type && args[0].type != ARGT_STR) ||
1724 (args[1].type && args[1].type != ARGT_STR))
1725 return 0;
1726
1727 name = "";
1728 name_len = 0;
1729 if (args->type == ARGT_STR) {
1730 name = args->data.str.area;
1731 name_len = args->data.str.data;
1732 }
1733
1734 if (args[1].type)
1735 delim = *args[1].data.str.area;
1736
1737 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001738 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1739 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001740
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001741 if (!htx)
1742 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001743
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001744 sl = http_get_stline(htx);
1745 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1746 if (!smp->ctx.a[0])
1747 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001748
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001749 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001750
1751 /* Assume that the context is filled with NULL pointer
1752 * before the first call.
1753 * smp->ctx.a[2] = NULL;
1754 * smp->ctx.a[3] = NULL;
1755 */
1756 }
1757
1758 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1759}
1760
1761/* This function iterates over each parameter of the body. This requires
1762 * that the body has been waited for using http-buffer-request. It uses
1763 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
1764 * contigous part of the body, and optionally ctx->a[2..3] to reference the
1765 * optional second part if the body wraps at the end of the buffer. An optional
1766 * parameter name is passed in args[0], otherwise any parameter is considered.
1767 */
1768static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1769{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001770 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001771 const char *name;
1772 int name_len;
1773
1774 if (!args || (args[0].type && args[0].type != ARGT_STR))
1775 return 0;
1776
1777 name = "";
1778 name_len = 0;
1779 if (args[0].type == ARGT_STR) {
1780 name = args[0].data.str.area;
1781 name_len = args[0].data.str.data;
1782 }
1783
1784 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001785 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1786 struct buffer *temp;
1787 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001788
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001789 if (!htx)
1790 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001791
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001792 temp = get_trash_chunk();
1793 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1794 struct htx_blk *blk = htx_get_blk(htx, pos);
1795 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001796
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001797 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1798 break;
1799 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001800 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001801 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001802 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001803 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001804
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001805 smp->ctx.a[0] = temp->area;
1806 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001807
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001808 /* Assume that the context is filled with NULL pointer
1809 * before the first call.
1810 * smp->ctx.a[2] = NULL;
1811 * smp->ctx.a[3] = NULL;
1812 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001813
Willy Tarreau79e57332018-10-02 16:01:16 +02001814 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001815
Willy Tarreau79e57332018-10-02 16:01:16 +02001816 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1817}
1818
1819/* Return the signed integer value for the specified url parameter (see url_param
1820 * above).
1821 */
1822static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1823{
1824 int ret = smp_fetch_url_param(args, smp, kw, private);
1825
1826 if (ret > 0) {
1827 smp->data.type = SMP_T_SINT;
1828 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1829 smp->data.u.str.data);
1830 }
1831
1832 return ret;
1833}
1834
1835/* This produces a 32-bit hash of the concatenation of the first occurrence of
1836 * the Host header followed by the path component if it begins with a slash ('/').
1837 * This means that '*' will not be added, resulting in exactly the first Host
1838 * entry. If no Host header is found, then the path is used. The resulting value
1839 * is hashed using the url hash followed by a full avalanche hash and provides a
1840 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1841 * high-traffic sites without having to store whole paths.
1842 * this differs from the base32 functions in that it includes the url parameters
1843 * as well as the path
1844 */
1845static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1846{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001847 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001848 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1849 struct http_hdr_ctx ctx;
1850 struct htx_sl *sl;
1851 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001852 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001853
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001854 if (!htx)
1855 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001856
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001857 ctx.blk = NULL;
1858 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1859 /* OK we have the header value in ctx.value */
1860 while (ctx.value.len--)
1861 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001862 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001863
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001864 /* now retrieve the path */
1865 sl = http_get_stline(htx);
1866 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001867 if (path.len && *(path.ptr) == '/') {
1868 while (path.len--)
1869 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001870 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001871
Willy Tarreau79e57332018-10-02 16:01:16 +02001872 hash = full_hash(hash);
1873
1874 smp->data.type = SMP_T_SINT;
1875 smp->data.u.sint = hash;
1876 smp->flags = SMP_F_VOL_1ST;
1877 return 1;
1878}
1879
1880/* This concatenates the source address with the 32-bit hash of the Host and
1881 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1882 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1883 * on the source address length. The URL hash is stored before the address so
1884 * that in environments where IPv6 is insignificant, truncating the output to
1885 * 8 bytes would still work.
1886 */
1887static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1888{
1889 struct buffer *temp;
1890 struct connection *cli_conn = objt_conn(smp->sess->origin);
1891
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001892 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001893 return 0;
1894
1895 if (!smp_fetch_url32(args, smp, kw, private))
1896 return 0;
1897
1898 temp = get_trash_chunk();
1899 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1900 temp->data += sizeof(unsigned int);
1901
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001902 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001903 case AF_INET:
1904 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001905 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001906 4);
1907 temp->data += 4;
1908 break;
1909 case AF_INET6:
1910 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001911 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001912 16);
1913 temp->data += 16;
1914 break;
1915 default:
1916 return 0;
1917 }
1918
1919 smp->data.u.str = *temp;
1920 smp->data.type = SMP_T_BIN;
1921 return 1;
1922}
1923
1924/************************************************************************/
1925/* Other utility functions */
1926/************************************************************************/
1927
1928/* This function is used to validate the arguments passed to any "hdr" fetch
1929 * keyword. These keywords support an optional positive or negative occurrence
1930 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
1931 * is assumed that the types are already the correct ones. Returns 0 on error,
1932 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
1933 * error message in case of error, that the caller is responsible for freeing.
1934 * The initial location must either be freeable or NULL.
1935 * Note: this function's pointer is checked from Lua.
1936 */
1937int val_hdr(struct arg *arg, char **err_msg)
1938{
1939 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
1940 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
1941 return 0;
1942 }
1943 return 1;
1944}
1945
1946/************************************************************************/
1947/* All supported sample fetch keywords must be declared here. */
1948/************************************************************************/
1949
1950/* Note: must not be declared <const> as its list will be overwritten */
1951static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
1952 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1953 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
1954 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
1955
1956 /* capture are allocated and are permanent in the stream */
1957 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
1958
1959 /* retrieve these captures from the HTTP logs */
1960 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1961 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1962 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1963
1964 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
1965 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1966
1967 /* cookie is valid in both directions (eg: for "stick ...") but cook*
1968 * are only here to match the ACL's name, are request-only and are used
1969 * for ACL compatibility only.
1970 */
1971 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001972 { "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 +02001973 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1974 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1975
1976 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
1977 * only here to match the ACL's name, are request-only and are used for
1978 * ACL compatibility only.
1979 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001980 { "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 +02001981 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1982 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
1983 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
1984
Christopher Fauleta4063562019-08-02 11:51:37 +02001985 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1986 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1987 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02001988 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
1989 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
1990 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
1991 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
1992 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1993 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1994
1995 /* HTTP protocol on the request path */
1996 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
1997 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
1998
1999 /* HTTP version on the request path */
2000 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2001 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2002
2003 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2004 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2005 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2006 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2007
2008 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2009 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2010
2011 /* HTTP version on the response path */
2012 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2013 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2014
2015 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2016 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2017 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2018 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2019
2020 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2021 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2022 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2023 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2024 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2025 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2026 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2027
2028 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2029 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2030 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2031 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2032
2033 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2034 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2035 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2036 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2037 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2038 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2039 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2040
2041 /* scook is valid only on the response and is used for ACL compatibility */
2042 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2043 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2044 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2045 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2046
2047 /* shdr is valid only on the response and is used for ACL compatibility */
2048 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2049 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2050 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2051 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2052
2053 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2054 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2055 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2056 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2057 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2058 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2059 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2060 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2061 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2062 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2063 { /* END */ },
2064}};
2065
Willy Tarreau0108d902018-11-25 19:14:37 +01002066INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002067
2068/*
2069 * Local variables:
2070 * c-indent-level: 8
2071 * c-basic-offset: 8
2072 * End:
2073 */