blob: 08e71f4b40d494387eae13f42868145a24c55673 [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 Tarreau79e57332018-10-02 16:01:16 +020037#include <proto/http_fetch.h>
Christopher Fauletef453ed2018-10-24 21:39:27 +020038#include <proto/http_htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020039#include <proto/log.h>
40#include <proto/obj_type.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020041#include <proto/http_ana.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020042#include <proto/sample.h>
43#include <proto/stream.h>
44
45
46/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Fauletef453ed2018-10-24 21:39:27 +020047static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
48
Christopher Faulet89dc4992019-04-17 12:02:59 +020049#define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL)
50#define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL)
Willy Tarreau79e57332018-10-02 16:01:16 +020051
52/*
53 * Returns the data from Authorization header. Function may be called more
54 * than once so data is stored in txn->auth_data. When no header is found
55 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
56 * searching again for something we are unable to find anyway. However, if
57 * the result if valid, the cache is not reused because we would risk to
58 * have the credentials overwritten by another stream in parallel.
59 */
60
Christopher Fauletcd761952019-07-15 13:58:29 +020061static int get_http_auth(struct sample *smp, struct htx *htx)
Willy Tarreau79e57332018-10-02 16:01:16 +020062{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020063 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020064 struct http_txn *txn = s->txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020065 struct http_hdr_ctx ctx = { .blk = NULL };
66 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020067 struct buffer auth_method;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020068 char *p;
Willy Tarreau79e57332018-10-02 16:01:16 +020069 int len;
70
71#ifdef DEBUG_AUTH
72 printf("Auth for stream %p: %d\n", s, txn->auth.method);
73#endif
Willy Tarreau79e57332018-10-02 16:01:16 +020074 if (txn->auth.method == HTTP_AUTH_WRONG)
75 return 0;
76
77 txn->auth.method = HTTP_AUTH_WRONG;
78
Christopher Faulet6d1dd462019-07-15 14:36:03 +020079 if (txn->flags & TX_USE_PX_CONN)
80 hdr = ist("Proxy-Authorization");
81 else
82 hdr = ist("Authorization");
Willy Tarreau79e57332018-10-02 16:01:16 +020083
Christopher Faulet6d1dd462019-07-15 14:36:03 +020084 ctx.blk = NULL;
85 if (!http_find_header(htx, hdr, &ctx, 0))
86 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +020087
Christopher Faulet6d1dd462019-07-15 14:36:03 +020088 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
89 len = p - ctx.value.ptr;
90 if (!p || len <= 0)
91 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +020092
Christopher Faulet6d1dd462019-07-15 14:36:03 +020093 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
94 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +020095
Christopher Faulet6d1dd462019-07-15 14:36:03 +020096 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +020097
98 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
99 struct buffer *http_auth = get_trash_chunk();
100
101 len = base64dec(txn->auth.method_data.area,
102 txn->auth.method_data.data,
103 http_auth->area, global.tune.bufsize - 1);
104
105 if (len < 0)
106 return 0;
107
108
109 http_auth->area[len] = '\0';
110
111 p = strchr(http_auth->area, ':');
112
113 if (!p)
114 return 0;
115
116 txn->auth.user = http_auth->area;
117 *p = '\0';
118 txn->auth.pass = p+1;
119
120 txn->auth.method = HTTP_AUTH_BASIC;
121 return 1;
122 }
123
124 return 0;
125}
126
127/* This function ensures that the prerequisites for an L7 fetch are ready,
128 * which means that a request or response is ready. If some data is missing,
129 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200130 * to extract data from L7. If <vol> is non-null during a prefetch, another
131 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200132 *
133 * The function returns :
134 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
135 * decide whether or not an HTTP message is present ;
136 * NULL if the requested data cannot be fetched or if it is certain that
137 * we'll never have any HTTP message there ;
138 * The HTX message if ready
139 */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200140struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200141{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200142 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200143 struct http_txn *txn = NULL;
144 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200145 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100146 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200147
148 /* Note: it is possible that <s> is NULL when called before stream
149 * initialization (eg: tcp-request connection), so this function is the
150 * one responsible for guarding against this case for all HTTP users.
151 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200152 if (!s || !chn)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200153 return NULL;
154
155 if (!s->txn) {
156 if (unlikely(!http_alloc_txn(s)))
157 return NULL; /* not enough memory */
158 http_init_txn(s);
159 txn = s->txn;
160 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200161 txn = s->txn;
162 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
163 smp->data.type = SMP_T_BOOL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200164
Christopher Fauleteca88542019-04-03 10:12:42 +0200165 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200166 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200167
Christopher Faulet89dc4992019-04-17 12:02:59 +0200168 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
169 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200170
Christopher Faulet89dc4992019-04-17 12:02:59 +0200171 if (msg->msg_state < HTTP_MSG_BODY) {
172 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200173 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200174 /* Parsing is done by the mux, just wait */
175 smp->flags |= SMP_F_MAY_CHANGE;
176 return NULL;
177 }
178 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200179 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200180 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200181 /* The start-line was already forwarded, it is too late to fetch anything */
182 return NULL;
183 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200184 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200185 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200186 struct buffer *buf;
187 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200188 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200189 union h1_sl h1sl;
190 unsigned int flags = HTX_FL_NONE;
191 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200192
Christopher Faulet89dc4992019-04-17 12:02:59 +0200193 /* no HTTP fetch on the response in TCP mode */
194 if (chn->flags & CF_ISRESP)
195 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200196
Christopher Faulet89dc4992019-04-17 12:02:59 +0200197 /* Now we are working on the request only */
198 buf = &chn->buf;
199 if (b_head(buf) + b_data(buf) > b_wrap(buf))
200 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200201
Christopher Faulet89dc4992019-04-17 12:02:59 +0200202 h1m_init_req(&h1m);
203 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
204 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
205 if (ret <= 0) {
206 /* Invalid or too big*/
207 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200208 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100209
Christopher Faulet89dc4992019-04-17 12:02:59 +0200210 /* wait for a full request */
211 smp->flags |= SMP_F_MAY_CHANGE;
212 return NULL;
213 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100214
Christopher Faulet89dc4992019-04-17 12:02:59 +0200215 /* OK we just got a valid HTTP mesage. We have to convert it
216 * into an HTX message.
217 */
218 if (unlikely(h1sl.rq.v.len == 0)) {
219 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
220 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200221 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200222 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200223 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200224
225 /* Set HTX start-line flags */
226 if (h1m.flags & H1_MF_VER_11)
227 flags |= HTX_SL_F_VER_11;
228 if (h1m.flags & H1_MF_XFER_ENC)
229 flags |= HTX_SL_F_XFER_ENC;
230 flags |= HTX_SL_F_XFER_LEN;
231 if (h1m.flags & H1_MF_CHNK)
232 flags |= HTX_SL_F_CHNK;
233 else if (h1m.flags & H1_MF_CLEN)
234 flags |= HTX_SL_F_CLEN;
235
236 htx = htx_from_buf(get_trash_chunk());
237 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
238 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200239 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200240 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200241 }
242
243 /* OK we just got a valid HTTP message. If not already done by
244 * HTTP analyzers, we have some minor preparation to perform so
245 * that further checks can rely on HTTP tests.
246 */
247 if (sl && msg->msg_state < HTTP_MSG_BODY) {
248 if (!(chn->flags & CF_ISRESP)) {
249 txn->meth = sl->info.req.meth;
250 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
251 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200252 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200253 else
254 txn->status = sl->info.res.status;
255 if (sl->flags & HTX_SL_F_VER_11)
256 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200257 }
258
259 /* everything's OK */
260 smp->data.u.sint = 1;
261 return htx;
262}
263
Willy Tarreau79e57332018-10-02 16:01:16 +0200264/* This function fetches the method of current HTTP request and stores
265 * it in the global pattern struct as a chunk. There are two possibilities :
266 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
267 * in <len> and <ptr> is NULL ;
268 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
269 * <len> to its length.
270 * This is intended to be used with pat_match_meth() only.
271 */
272static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
273{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200274 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200275 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +0200276 struct http_txn *txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200277 int meth;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200278
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200279 if (!htx)
280 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200281
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200282 txn = smp->strm->txn;
283 meth = txn->meth;
284 smp->data.type = SMP_T_METH;
285 smp->data.u.meth.meth = meth;
286 if (meth == HTTP_METH_OTHER) {
287 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200288
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200289 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
290 /* ensure the indexes are not affected */
291 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200292 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200293 sl = http_get_stline(htx);
294 smp->flags |= SMP_F_CONST;
295 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
296 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200297 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200298 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200299 return 1;
300}
301
302static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
303{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200304 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200305 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
306 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200307 char *ptr;
308 int len;
309
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200310 if (!htx)
311 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200312
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200313 sl = http_get_stline(htx);
314 len = HTX_SL_REQ_VLEN(sl);
315 ptr = HTX_SL_REQ_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200316
317 while ((len-- > 0) && (*ptr++ != '/'));
318 if (len <= 0)
319 return 0;
320
321 smp->data.type = SMP_T_STR;
322 smp->data.u.str.area = ptr;
323 smp->data.u.str.data = len;
324
325 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
326 return 1;
327}
328
329static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
330{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200331 struct channel *chn = SMP_RES_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200332 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
333 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200334 char *ptr;
335 int len;
336
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200337 if (!htx)
338 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200339
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200340 sl = http_get_stline(htx);
341 len = HTX_SL_RES_VLEN(sl);
342 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200343
344 while ((len-- > 0) && (*ptr++ != '/'));
345 if (len <= 0)
346 return 0;
347
348 smp->data.type = SMP_T_STR;
349 smp->data.u.str.area = ptr;
350 smp->data.u.str.data = len;
351
352 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
353 return 1;
354}
355
356/* 3. Check on Status Code. We manipulate integers here. */
357static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
358{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200359 struct channel *chn = SMP_RES_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200360 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
361 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200362 char *ptr;
363 int len;
364
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200365 if (!htx)
366 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200367
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200368 sl = http_get_stline(htx);
369 len = HTX_SL_RES_CLEN(sl);
370 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200371
372 smp->data.type = SMP_T_SINT;
373 smp->data.u.sint = __strl2ui(ptr, len);
374 smp->flags = SMP_F_VOL_1ST;
375 return 1;
376}
377
378static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
379{
380 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
381 return 0;
382
383 if (!smp->strm->unique_id) {
384 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
385 return 0;
386 smp->strm->unique_id[0] = '\0';
387 }
388 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
389 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
390
391 smp->data.type = SMP_T_STR;
392 smp->data.u.str.area = smp->strm->unique_id;
393 smp->flags = SMP_F_CONST;
394 return 1;
395}
396
397/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800398 * empty line which separes headers from the body. This is useful
399 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200400 */
401static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
402{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200403 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200404 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
405 struct buffer *temp;
406 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200407
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200408 if (!htx)
409 return 0;
410 temp = get_trash_chunk();
411 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
412 struct htx_blk *blk = htx_get_blk(htx, pos);
413 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200414
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200415 if (type == HTX_BLK_HDR) {
416 struct ist n = htx_get_blk_name(htx, blk);
417 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200418
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200419 if (!htx_hdr_to_h1(n, v, temp))
420 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200421 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200422 else if (type == HTX_BLK_EOH) {
423 if (!chunk_memcat(temp, "\r\n", 2))
424 return 0;
425 break;
426 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200427 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200428 smp->data.type = SMP_T_STR;
429 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200430 return 1;
431}
432
433/* Returns the header request in a length/value encoded format.
434 * This is useful for exchanges with the SPOE.
435 *
436 * A "length value" is a multibyte code encoding numbers. It uses the
437 * SPOE format. The encoding is the following:
438 *
439 * Each couple "header name" / "header value" is composed
440 * like this:
441 * "length value" "header name bytes"
442 * "length value" "header value bytes"
443 * When the last header is reached, the header name and the header
444 * value are empty. Their length are 0
445 */
446static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
447{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200448 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200449 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200450 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200451 char *p, *end;
452 int32_t pos;
453 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200454
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200455 if (!htx)
456 return 0;
457 temp = get_trash_chunk();
458 p = temp->area;
459 end = temp->area + temp->size;
460 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
461 struct htx_blk *blk = htx_get_blk(htx, pos);
462 enum htx_blk_type type = htx_get_blk_type(blk);
463 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200464
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200465 if (type == HTX_BLK_HDR) {
466 n = htx_get_blk_name(htx,blk);
467 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200468
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200469 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200470 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200471 if (ret == -1)
472 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200473 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200474 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200475 memcpy(p, n.ptr, n.len);
476 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200477
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200478 /* encode the header value. */
479 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200480 if (ret == -1)
481 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200482 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200483 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200484 memcpy(p, v.ptr, v.len);
485 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200486
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200487 }
488 else if (type == HTX_BLK_EOH) {
489 /* encode the end of the header list with empty
490 * header name and header value.
491 */
492 ret = encode_varint(0, &p, end);
493 if (ret == -1)
494 return 0;
495 ret = encode_varint(0, &p, end);
496 if (ret == -1)
497 return 0;
498 break;
499 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200500 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200501
502 /* Initialise sample data which will be filled. */
503 smp->data.type = SMP_T_BIN;
504 smp->data.u.str.area = temp->area;
505 smp->data.u.str.data = p - temp->area;
506 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200507 return 1;
508}
509
510/* returns the longest available part of the body. This requires that the body
511 * has been waited for using http-buffer-request.
512 */
513static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
514{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200515 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200516 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200517 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200518 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200519
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200520 if (!htx)
521 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200522
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200523 temp = get_trash_chunk();
524 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
525 struct htx_blk *blk = htx_get_blk(htx, pos);
526 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200527
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200528 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
529 break;
530 if (type == HTX_BLK_DATA) {
531 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
532 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200533 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200534 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200535
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200536 smp->data.type = SMP_T_BIN;
537 smp->data.u.str = *temp;
538 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200539 return 1;
540}
541
542
543/* returns the available length of the body. This requires that the body
544 * has been waited for using http-buffer-request.
545 */
546static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
547{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200548 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200549 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
550 int32_t pos;
551 unsigned long long len = 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100552
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200553 if (!htx)
554 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100555
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200556 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);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100559
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)
563 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200564 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200565
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200566 smp->data.type = SMP_T_SINT;
567 smp->data.u.sint = len;
568 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200569 return 1;
570}
571
572
573/* returns the advertised length of the body, or the advertised size of the
574 * chunks available in the buffer. This requires that the body has been waited
575 * for using http-buffer-request.
576 */
577static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
578{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200579 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200580 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
581 int32_t pos;
582 unsigned long long len = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200583
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200584 if (!htx)
585 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100586
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200587 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
588 struct htx_blk *blk = htx_get_blk(htx, pos);
589 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100590
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200591 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
592 break;
593 if (type == HTX_BLK_DATA)
594 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200595 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200596 if (htx->extra != ULLONG_MAX)
597 len += htx->extra;
Willy Tarreau79e57332018-10-02 16:01:16 +0200598
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200599 smp->data.type = SMP_T_SINT;
600 smp->data.u.sint = len;
601 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200602 return 1;
603}
604
605
606/* 4. Check on URL/URI. A pointer to the URI is stored. */
607static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
608{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200609 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200610 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
611 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200612
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200613 if (!htx)
614 return 0;
615 sl = http_get_stline(htx);
616 smp->data.type = SMP_T_STR;
617 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
618 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
619 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200620 return 1;
621}
622
623static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
624{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200625 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200626 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
627 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200628 struct sockaddr_storage addr;
629
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200630 if (!htx)
631 return 0;
632 sl = http_get_stline(htx);
633 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +0200634
Willy Tarreau79e57332018-10-02 16:01:16 +0200635 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
636 return 0;
637
638 smp->data.type = SMP_T_IPV4;
639 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
640 smp->flags = 0;
641 return 1;
642}
643
644static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
645{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200646 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200647 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
648 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200649 struct sockaddr_storage addr;
650
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200651 if (!htx)
652 return 0;
653 sl = http_get_stline(htx);
654 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200655
Willy Tarreau79e57332018-10-02 16:01:16 +0200656 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
657 return 0;
658
659 smp->data.type = SMP_T_SINT;
660 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
661 smp->flags = 0;
662 return 1;
663}
664
665/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
666 * Accepts an optional argument of type string containing the header field name,
667 * and an optional argument of type signed or unsigned integer to request an
668 * explicit occurrence of the header. Note that in the event of a missing name,
669 * headers are considered from the first one. It does not stop on commas and
670 * returns full lines instead (useful for User-Agent or Date for example).
671 */
672static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
673{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200674 /* possible keywords: req.fhdr, res.fhdr */
675 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200676 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
677 struct http_hdr_ctx *ctx = smp->ctx.a[0];
678 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200679 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200680
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200681 if (!ctx) {
682 /* first call */
683 ctx = &static_http_hdr_ctx;
684 ctx->blk = NULL;
685 smp->ctx.a[0] = ctx;
686 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200687
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200688 if (args) {
689 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200690 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200691 name.ptr = args[0].data.str.area;
692 name.len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +0200693
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200694 if (args[1].type == ARGT_SINT)
695 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200696 }
697
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200698 if (!htx)
699 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200700
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200701 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
702 /* search for header from the beginning */
703 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200704
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200705 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
706 /* no explicit occurrence and single fetch => last header by default */
707 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200708
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200709 if (!occ)
710 /* prepare to report multiple occurrences for ACL fetches */
711 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200712
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200713 smp->data.type = SMP_T_STR;
714 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
715 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
716 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200717 smp->flags &= ~SMP_F_NOT_LAST;
718 return 0;
719}
720
721/* 6. Check on HTTP header count. The number of occurrences is returned.
722 * Accepts exactly 1 argument of type string. It does not stop on commas and
723 * returns full lines instead (useful for User-Agent or Date for example).
724 */
725static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
726{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200727 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
728 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200729 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
730 struct http_hdr_ctx ctx;
731 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200732 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200733
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200734 if (!htx)
735 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200736
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200737 if (args && args->type == ARGT_STR) {
738 name.ptr = args->data.str.area;
739 name.len = args->data.str.data;
740 } else {
741 name.ptr = NULL;
742 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200743 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200744
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200745 ctx.blk = NULL;
746 cnt = 0;
747 while (http_find_header(htx, name, &ctx, 1))
748 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200749 smp->data.type = SMP_T_SINT;
750 smp->data.u.sint = cnt;
751 smp->flags = SMP_F_VOL_HDR;
752 return 1;
753}
754
755static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
756{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200757 /* possible keywords: req.hdr_names, res.hdr_names */
758 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200759 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200760 struct buffer *temp;
761 char del = ',';
762
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200763 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200764
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200765 if (!htx)
766 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200767
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200768 if (args && args->type == ARGT_STR)
769 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200770
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200771 temp = get_trash_chunk();
772 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
773 struct htx_blk *blk = htx_get_blk(htx, pos);
774 enum htx_blk_type type = htx_get_blk_type(blk);
775 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200776
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200777 if (type == HTX_BLK_EOH)
778 break;
779 if (type != HTX_BLK_HDR)
780 continue;
781 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200782
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200783 if (temp->data)
784 temp->area[temp->data++] = del;
785 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200786 }
787
788 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200789 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200790 smp->flags = SMP_F_VOL_HDR;
791 return 1;
792}
793
794/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
795 * Accepts an optional argument of type string containing the header field name,
796 * and an optional argument of type signed or unsigned integer to request an
797 * explicit occurrence of the header. Note that in the event of a missing name,
798 * headers are considered from the first one.
799 */
800static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
801{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200802 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
803 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200804 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
805 struct http_hdr_ctx *ctx = smp->ctx.a[0];
806 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200807 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200808
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200809 if (!ctx) {
810 /* first call */
811 ctx = &static_http_hdr_ctx;
812 ctx->blk = NULL;
813 smp->ctx.a[0] = ctx;
814 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200815
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200816 if (args) {
817 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200818 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200819 name.ptr = args[0].data.str.area;
820 name.len = args[0].data.str.data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200821
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200822 if (args[1].type == ARGT_SINT)
823 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200824 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200825
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200826 if (!htx)
827 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200828
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200829 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
830 /* search for header from the beginning */
831 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200832
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200833 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
834 /* no explicit occurrence and single fetch => last header by default */
835 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200836
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200837 if (!occ)
838 /* prepare to report multiple occurrences for ACL fetches */
839 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200840
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200841 smp->data.type = SMP_T_STR;
842 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
843 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
844 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200845
846 smp->flags &= ~SMP_F_NOT_LAST;
847 return 0;
848}
849
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200850/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
851 * the right channel. So instead of duplicating the code, we just change the
852 * keyword and then fallback on smp_fetch_hdr().
853 */
854static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
855{
856 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
857 return smp_fetch_hdr(args, smp, kw, private);
858}
859
Willy Tarreau79e57332018-10-02 16:01:16 +0200860/* 6. Check on HTTP header count. The number of occurrences is returned.
861 * Accepts exactly 1 argument of type string.
862 */
863static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
864{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200865 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
866 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200867 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
868 struct http_hdr_ctx ctx;
869 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200870 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200871
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200872 if (!htx)
873 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200874
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200875 if (args && args->type == ARGT_STR) {
876 name.ptr = args->data.str.area;
877 name.len = args->data.str.data;
878 } else {
879 name.ptr = NULL;
880 name.len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200881 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200882
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200883 ctx.blk = NULL;
884 cnt = 0;
885 while (http_find_header(htx, name, &ctx, 0))
886 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200887
888 smp->data.type = SMP_T_SINT;
889 smp->data.u.sint = cnt;
890 smp->flags = SMP_F_VOL_HDR;
891 return 1;
892}
893
894/* Fetch an HTTP header's integer value. The integer value is returned. It
895 * takes a mandatory argument of type string and an optional one of type int
896 * to designate a specific occurrence. It returns an unsigned integer, which
897 * may or may not be appropriate for everything.
898 */
899static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
900{
901 int ret = smp_fetch_hdr(args, smp, kw, private);
902
903 if (ret > 0) {
904 smp->data.type = SMP_T_SINT;
905 smp->data.u.sint = strl2ic(smp->data.u.str.area,
906 smp->data.u.str.data);
907 }
908
909 return ret;
910}
911
912/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
913 * and an optional one of type int to designate a specific occurrence.
914 * It returns an IPv4 or IPv6 address.
915 */
916static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
917{
918 int ret;
919
920 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
921 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
922 smp->data.type = SMP_T_IPV4;
923 break;
924 } else {
925 struct buffer *temp = get_trash_chunk();
926 if (smp->data.u.str.data < temp->size - 1) {
927 memcpy(temp->area, smp->data.u.str.area,
928 smp->data.u.str.data);
929 temp->area[smp->data.u.str.data] = '\0';
930 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
931 smp->data.type = SMP_T_IPV6;
932 break;
933 }
934 }
935 }
936
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200937 /* if the header doesn't match an IP address, fetch next one */
938 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200939 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200940 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200941 return ret;
942}
Willy Tarreau79e57332018-10-02 16:01:16 +0200943
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200944/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
945 * the first '/' after the possible hostname, and ends before the possible '?'.
946 */
947static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
948{
949 struct channel *chn = SMP_REQ_CHN(smp);
950 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
951 struct htx_sl *sl;
952 struct ist path;
953 size_t len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200954
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200955 if (!htx)
956 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200957
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200958 sl = http_get_stline(htx);
959 path = http_get_path(htx_sl_req_uri(sl));
960 if (!path.ptr)
961 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200962
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200963 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
964 ;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200965
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200966 /* OK, we got the '/' ! */
967 smp->data.type = SMP_T_STR;
968 smp->data.u.str.area = path.ptr;
969 smp->data.u.str.data = len;
970 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200971 return 1;
972}
973
974/* This produces a concatenation of the first occurrence of the Host header
975 * followed by the path component if it begins with a slash ('/'). This means
976 * that '*' will not be added, resulting in exactly the first Host entry.
977 * If no Host header is found, then the path is returned as-is. The returned
978 * value is stored in the trash so it does not need to be marked constant.
979 * The returned sample is of type string.
980 */
981static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
982{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200983 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200984 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
985 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200986 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200987 struct http_hdr_ctx ctx;
988 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200989
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200990 if (!htx)
991 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200992
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200993 ctx.blk = NULL;
994 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
995 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200996
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200997 /* OK we have the header value in ctx.value */
998 temp = get_trash_chunk();
999 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001000
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001001 /* now retrieve the path */
1002 sl = http_get_stline(htx);
1003 path = http_get_path(htx_sl_req_uri(sl));
1004 if (path.ptr) {
1005 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001006
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001007 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1008 ;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001009
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001010 if (len && *(path.ptr) == '/')
1011 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001012 }
1013
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001014 smp->data.type = SMP_T_STR;
1015 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001016 smp->flags = SMP_F_VOL_1ST;
1017 return 1;
1018}
1019
1020/* This produces a 32-bit hash of the concatenation of the first occurrence of
1021 * the Host header followed by the path component if it begins with a slash ('/').
1022 * This means that '*' will not be added, resulting in exactly the first Host
1023 * entry. If no Host header is found, then the path is used. The resulting value
1024 * is hashed using the path hash followed by a full avalanche hash and provides a
1025 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1026 * high-traffic sites without having to store whole paths.
1027 */
1028static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1029{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001030 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001031 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1032 struct htx_sl *sl;
1033 struct http_hdr_ctx ctx;
1034 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001035 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001036
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001037 if (!htx)
1038 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001039
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001040 ctx.blk = NULL;
1041 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1042 /* OK we have the header value in ctx.value */
1043 while (ctx.value.len--)
1044 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001045 }
1046
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001047 /* now retrieve the path */
1048 sl = http_get_stline(htx);
1049 path = http_get_path(htx_sl_req_uri(sl));
1050 if (path.ptr) {
1051 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001052
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001053 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1054 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001055
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001056 if (len && *(path.ptr) == '/') {
1057 while (len--)
1058 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001059 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001060 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001061
Willy Tarreau79e57332018-10-02 16:01:16 +02001062 hash = full_hash(hash);
1063
1064 smp->data.type = SMP_T_SINT;
1065 smp->data.u.sint = hash;
1066 smp->flags = SMP_F_VOL_1ST;
1067 return 1;
1068}
1069
1070/* This concatenates the source address with the 32-bit hash of the Host and
1071 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1072 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1073 * on the source address length. The path hash is stored before the address so
1074 * that in environments where IPv6 is insignificant, truncating the output to
1075 * 8 bytes would still work.
1076 */
1077static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1078{
1079 struct buffer *temp;
1080 struct connection *cli_conn = objt_conn(smp->sess->origin);
1081
1082 if (!cli_conn)
1083 return 0;
1084
1085 if (!smp_fetch_base32(args, smp, kw, private))
1086 return 0;
1087
1088 temp = get_trash_chunk();
1089 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1090 temp->data += sizeof(unsigned int);
1091
1092 switch (cli_conn->addr.from.ss_family) {
1093 case AF_INET:
1094 memcpy(temp->area + temp->data,
1095 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1096 4);
1097 temp->data += 4;
1098 break;
1099 case AF_INET6:
1100 memcpy(temp->area + temp->data,
1101 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1102 16);
1103 temp->data += 16;
1104 break;
1105 default:
1106 return 0;
1107 }
1108
1109 smp->data.u.str = *temp;
1110 smp->data.type = SMP_T_BIN;
1111 return 1;
1112}
1113
1114/* Extracts the query string, which comes after the question mark '?'. If no
1115 * question mark is found, nothing is returned. Otherwise it returns a sample
1116 * of type string carrying the whole query string.
1117 */
1118static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1119{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001120 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001121 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1122 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001123 char *ptr, *end;
1124
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001125 if (!htx)
1126 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001127
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001128 sl = http_get_stline(htx);
1129 ptr = HTX_SL_REQ_UPTR(sl);
1130 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001131
1132 /* look up the '?' */
1133 do {
1134 if (ptr == end)
1135 return 0;
1136 } while (*ptr++ != '?');
1137
1138 smp->data.type = SMP_T_STR;
1139 smp->data.u.str.area = ptr;
1140 smp->data.u.str.data = end - ptr;
1141 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1142 return 1;
1143}
1144
1145static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1146{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001147 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001148 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001149
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001150 if (!htx)
1151 return 0;
1152 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001153 smp->data.u.sint = 1;
1154 return 1;
1155}
1156
1157/* return a valid test if the current request is the first one on the connection */
1158static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1159{
1160 smp->data.type = SMP_T_BOOL;
1161 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1162 return 1;
1163}
1164
1165/* Accepts exactly 1 argument of type userlist */
1166static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1167{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001168 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001169 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001170
1171 if (!args || args->type != ARGT_USR)
1172 return 0;
1173
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001174 if (!htx)
1175 return 0;
1176 if (!get_http_auth(smp, htx))
1177 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001178
1179 smp->data.type = SMP_T_BOOL;
1180 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001181 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001182 return 1;
1183}
1184
1185/* Accepts exactly 1 argument of type userlist */
1186static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1187{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001188 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001189 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001190
Willy Tarreau79e57332018-10-02 16:01:16 +02001191 if (!args || args->type != ARGT_USR)
1192 return 0;
1193
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001194 if (!htx)
1195 return 0;
1196 if (!get_http_auth(smp, htx))
1197 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001198
Willy Tarreau79e57332018-10-02 16:01:16 +02001199 /* if the user does not belong to the userlist or has a wrong password,
1200 * report that it unconditionally does not match. Otherwise we return
1201 * a string containing the username.
1202 */
1203 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1204 smp->strm->txn->auth.pass))
1205 return 0;
1206
1207 /* pat_match_auth() will need the user list */
1208 smp->ctx.a[0] = args->data.usr;
1209
1210 smp->data.type = SMP_T_STR;
1211 smp->flags = SMP_F_CONST;
1212 smp->data.u.str.area = smp->strm->txn->auth.user;
1213 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1214
1215 return 1;
1216}
1217
1218/* Fetch a captured HTTP request header. The index is the position of
1219 * the "capture" option in the configuration file
1220 */
1221static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1222{
1223 struct proxy *fe = strm_fe(smp->strm);
1224 int idx;
1225
1226 if (!args || args->type != ARGT_SINT)
1227 return 0;
1228
1229 idx = args->data.sint;
1230
1231 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1232 return 0;
1233
1234 smp->data.type = SMP_T_STR;
1235 smp->flags |= SMP_F_CONST;
1236 smp->data.u.str.area = smp->strm->req_cap[idx];
1237 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1238
1239 return 1;
1240}
1241
1242/* Fetch a captured HTTP response header. The index is the position of
1243 * the "capture" option in the configuration file
1244 */
1245static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1246{
1247 struct proxy *fe = strm_fe(smp->strm);
1248 int idx;
1249
1250 if (!args || args->type != ARGT_SINT)
1251 return 0;
1252
1253 idx = args->data.sint;
1254
1255 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1256 return 0;
1257
1258 smp->data.type = SMP_T_STR;
1259 smp->flags |= SMP_F_CONST;
1260 smp->data.u.str.area = smp->strm->res_cap[idx];
1261 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1262
1263 return 1;
1264}
1265
1266/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1267static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1268{
1269 struct buffer *temp;
1270 struct http_txn *txn = smp->strm->txn;
1271 char *ptr;
1272
1273 if (!txn || !txn->uri)
1274 return 0;
1275
1276 ptr = txn->uri;
1277
1278 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1279 ptr++;
1280
1281 temp = get_trash_chunk();
1282 temp->area = txn->uri;
1283 temp->data = ptr - txn->uri;
1284 smp->data.u.str = *temp;
1285 smp->data.type = SMP_T_STR;
1286 smp->flags = SMP_F_CONST;
1287
1288 return 1;
1289
1290}
1291
1292/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1293static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1294{
1295 struct http_txn *txn = smp->strm->txn;
1296 struct ist path;
1297 const char *ptr;
1298
1299 if (!txn || !txn->uri)
1300 return 0;
1301
1302 ptr = txn->uri;
1303
1304 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1305 ptr++;
1306
1307 if (!*ptr)
1308 return 0;
1309
Christopher Faulet78337bb2018-11-15 14:35:18 +01001310 /* skip the first space and find space after URI */
1311 path = ist2(++ptr, 0);
1312 while (*ptr != ' ' && *ptr != '\0')
1313 ptr++;
1314 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001315
Christopher Faulet78337bb2018-11-15 14:35:18 +01001316 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02001317 if (!path.ptr)
1318 return 0;
1319
1320 smp->data.u.str.area = path.ptr;
1321 smp->data.u.str.data = path.len;
1322 smp->data.type = SMP_T_STR;
1323 smp->flags = SMP_F_CONST;
1324
1325 return 1;
1326}
1327
1328/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1329 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1330 */
1331static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1332{
1333 struct http_txn *txn = smp->strm->txn;
1334
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001335 if (!txn || txn->req.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001336 return 0;
1337
1338 if (txn->req.flags & HTTP_MSGF_VER_11)
1339 smp->data.u.str.area = "HTTP/1.1";
1340 else
1341 smp->data.u.str.area = "HTTP/1.0";
1342
1343 smp->data.u.str.data = 8;
1344 smp->data.type = SMP_T_STR;
1345 smp->flags = SMP_F_CONST;
1346 return 1;
1347
1348}
1349
1350/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1351 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1352 */
1353static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1354{
1355 struct http_txn *txn = smp->strm->txn;
1356
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001357 if (!txn || txn->rsp.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001358 return 0;
1359
1360 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1361 smp->data.u.str.area = "HTTP/1.1";
1362 else
1363 smp->data.u.str.area = "HTTP/1.0";
1364
1365 smp->data.u.str.data = 8;
1366 smp->data.type = SMP_T_STR;
1367 smp->flags = SMP_F_CONST;
1368 return 1;
1369
1370}
1371
1372/* Iterate over all cookies present in a message. The context is stored in
1373 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1374 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1375 * the direction, multiple cookies may be parsed on the same line or not.
1376 * The cookie name is in args and the name length in args->data.str.len.
1377 * Accepts exactly 1 argument of type string. If the input options indicate
1378 * that no iterating is desired, then only last value is fetched if any.
1379 * The returned sample is of type CSTR. Can be used to parse cookies in other
1380 * files.
1381 */
1382static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1383{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001384 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1385 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001386 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1387 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1388 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001389 int occ = 0;
1390 int found = 0;
1391
1392 if (!args || args->type != ARGT_STR)
1393 return 0;
1394
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001395 if (!ctx) {
1396 /* first call */
1397 ctx = &static_http_hdr_ctx;
1398 ctx->blk = NULL;
1399 smp->ctx.a[2] = ctx;
1400 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001401
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001402 if (!htx)
1403 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001404
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001405 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001406
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001407 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1408 /* no explicit occurrence and single fetch => last cookie by default */
1409 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001410
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001411 /* OK so basically here, either we want only one value and it's the
1412 * last one, or we want to iterate over all of them and we fetch the
1413 * next one.
1414 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001415
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001416 if (!(smp->flags & SMP_F_NOT_LAST)) {
1417 /* search for the header from the beginning, we must first initialize
1418 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001419 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001420 smp->ctx.a[0] = NULL;
1421 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001422 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001423
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001424 smp->flags |= SMP_F_VOL_HDR;
1425 while (1) {
1426 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1427 if (!smp->ctx.a[0]) {
1428 if (!http_find_header(htx, hdr, ctx, 0))
1429 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001430
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001431 if (ctx->value.len < args->data.str.data + 1)
1432 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001433
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001434 smp->ctx.a[0] = ctx->value.ptr;
1435 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001436 }
1437
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001438 smp->data.type = SMP_T_STR;
1439 smp->flags |= SMP_F_CONST;
1440 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
1441 args->data.str.area, args->data.str.data,
1442 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1443 &smp->data.u.str.area,
1444 &smp->data.u.str.data);
1445 if (smp->ctx.a[0]) {
1446 found = 1;
1447 if (occ >= 0) {
1448 /* one value was returned into smp->data.u.str.{str,len} */
1449 smp->flags |= SMP_F_NOT_LAST;
1450 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001451 }
1452 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001453 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001454 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001455
Willy Tarreau79e57332018-10-02 16:01:16 +02001456 /* all cookie headers and values were scanned. If we're looking for the
1457 * last occurrence, we may return it now.
1458 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001459 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001460 smp->flags &= ~SMP_F_NOT_LAST;
1461 return found;
1462}
1463
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001464/* Same than smp_fetch_cookie() but only relies on the sample direction to
1465 * choose the right channel. So instead of duplicating the code, we just change
1466 * the keyword and then fallback on smp_fetch_cookie().
1467 */
1468static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1469{
1470 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1471 return smp_fetch_cookie(args, smp, kw, private);
1472}
1473
Willy Tarreau79e57332018-10-02 16:01:16 +02001474/* Iterate over all cookies present in a request to count how many occurrences
1475 * match the name in args and args->data.str.len. If <multi> is non-null, then
1476 * multiple cookies may be parsed on the same line. The returned sample is of
1477 * type UINT. Accepts exactly 1 argument of type string.
1478 */
1479static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1480{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001481 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1482 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001483 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1484 struct http_hdr_ctx ctx;
1485 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001486 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001487 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001488
1489 if (!args || args->type != ARGT_STR)
1490 return 0;
1491
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001492 if (!htx)
1493 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001494
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001495 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001496
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001497 val_end = val_beg = NULL;
1498 ctx.blk = NULL;
1499 cnt = 0;
1500 while (1) {
1501 /* Note: val_beg == NULL every time we need to fetch a new header */
1502 if (!val_beg) {
1503 if (!http_find_header(htx, hdr, &ctx, 0))
1504 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001505
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001506 if (ctx.value.len < args->data.str.data + 1)
1507 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001508
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001509 val_beg = ctx.value.ptr;
1510 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001511 }
1512
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001513 smp->data.type = SMP_T_STR;
1514 smp->flags |= SMP_F_CONST;
1515 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
1516 args->data.str.area, args->data.str.data,
1517 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1518 &smp->data.u.str.area,
1519 &smp->data.u.str.data))) {
1520 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001521 }
1522 }
1523
1524 smp->data.type = SMP_T_SINT;
1525 smp->data.u.sint = cnt;
1526 smp->flags |= SMP_F_VOL_HDR;
1527 return 1;
1528}
1529
1530/* Fetch an cookie's integer value. The integer value is returned. It
1531 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1532 */
1533static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1534{
1535 int ret = smp_fetch_cookie(args, smp, kw, private);
1536
1537 if (ret > 0) {
1538 smp->data.type = SMP_T_SINT;
1539 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1540 smp->data.u.str.data);
1541 }
1542
1543 return ret;
1544}
1545
1546/************************************************************************/
1547/* The code below is dedicated to sample fetches */
1548/************************************************************************/
1549
1550/* This scans a URL-encoded query string. It takes an optionally wrapping
1551 * string whose first contigous chunk has its beginning in ctx->a[0] and end
1552 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1553 * pointers are updated for next iteration before leaving.
1554 */
1555static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1556{
1557 const char *vstart, *vend;
1558 struct buffer *temp;
1559 const char **chunks = (const char **)smp->ctx.a;
1560
1561 if (!http_find_next_url_param(chunks, name, name_len,
1562 &vstart, &vend, delim))
1563 return 0;
1564
1565 /* Create sample. If the value is contiguous, return the pointer as CONST,
1566 * if the value is wrapped, copy-it in a buffer.
1567 */
1568 smp->data.type = SMP_T_STR;
1569 if (chunks[2] &&
1570 vstart >= chunks[0] && vstart <= chunks[1] &&
1571 vend >= chunks[2] && vend <= chunks[3]) {
1572 /* Wrapped case. */
1573 temp = get_trash_chunk();
1574 memcpy(temp->area, vstart, chunks[1] - vstart);
1575 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1576 vend - chunks[2]);
1577 smp->data.u.str.area = temp->area;
1578 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1579 } else {
1580 /* Contiguous case. */
1581 smp->data.u.str.area = (char *)vstart;
1582 smp->data.u.str.data = vend - vstart;
1583 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1584 }
1585
1586 /* Update context, check wrapping. */
1587 chunks[0] = vend;
1588 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1589 chunks[1] = chunks[3];
1590 chunks[2] = NULL;
1591 }
1592
1593 if (chunks[0] < chunks[1])
1594 smp->flags |= SMP_F_NOT_LAST;
1595
1596 return 1;
1597}
1598
1599/* This function iterates over each parameter of the query string. It uses
1600 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1601 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1602 * An optional parameter name is passed in args[0], otherwise any parameter is
1603 * considered. It supports an optional delimiter argument for the beginning of
1604 * the string in args[1], which defaults to "?".
1605 */
1606static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1607{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001608 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001609 char delim = '?';
1610 const char *name;
1611 int name_len;
1612
1613 if (!args ||
1614 (args[0].type && args[0].type != ARGT_STR) ||
1615 (args[1].type && args[1].type != ARGT_STR))
1616 return 0;
1617
1618 name = "";
1619 name_len = 0;
1620 if (args->type == ARGT_STR) {
1621 name = args->data.str.area;
1622 name_len = args->data.str.data;
1623 }
1624
1625 if (args[1].type)
1626 delim = *args[1].data.str.area;
1627
1628 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001629 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1630 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001631
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001632 if (!htx)
1633 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001634
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001635 sl = http_get_stline(htx);
1636 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1637 if (!smp->ctx.a[0])
1638 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001639
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001640 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001641
1642 /* Assume that the context is filled with NULL pointer
1643 * before the first call.
1644 * smp->ctx.a[2] = NULL;
1645 * smp->ctx.a[3] = NULL;
1646 */
1647 }
1648
1649 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1650}
1651
1652/* This function iterates over each parameter of the body. This requires
1653 * that the body has been waited for using http-buffer-request. It uses
1654 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
1655 * contigous part of the body, and optionally ctx->a[2..3] to reference the
1656 * optional second part if the body wraps at the end of the buffer. An optional
1657 * parameter name is passed in args[0], otherwise any parameter is considered.
1658 */
1659static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1660{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001661 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001662 const char *name;
1663 int name_len;
1664
1665 if (!args || (args[0].type && args[0].type != ARGT_STR))
1666 return 0;
1667
1668 name = "";
1669 name_len = 0;
1670 if (args[0].type == ARGT_STR) {
1671 name = args[0].data.str.area;
1672 name_len = args[0].data.str.data;
1673 }
1674
1675 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001676 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1677 struct buffer *temp;
1678 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001679
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001680 if (!htx)
1681 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001682
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001683 temp = get_trash_chunk();
1684 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1685 struct htx_blk *blk = htx_get_blk(htx, pos);
1686 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001687
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001688 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1689 break;
1690 if (type == HTX_BLK_DATA) {
1691 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
1692 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001693 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001694 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001695
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001696 smp->ctx.a[0] = temp->area;
1697 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001698
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001699 /* Assume that the context is filled with NULL pointer
1700 * before the first call.
1701 * smp->ctx.a[2] = NULL;
1702 * smp->ctx.a[3] = NULL;
1703 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001704
Willy Tarreau79e57332018-10-02 16:01:16 +02001705 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001706
Willy Tarreau79e57332018-10-02 16:01:16 +02001707 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1708}
1709
1710/* Return the signed integer value for the specified url parameter (see url_param
1711 * above).
1712 */
1713static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1714{
1715 int ret = smp_fetch_url_param(args, smp, kw, private);
1716
1717 if (ret > 0) {
1718 smp->data.type = SMP_T_SINT;
1719 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1720 smp->data.u.str.data);
1721 }
1722
1723 return ret;
1724}
1725
1726/* This produces a 32-bit hash of the concatenation of the first occurrence of
1727 * the Host header followed by the path component if it begins with a slash ('/').
1728 * This means that '*' will not be added, resulting in exactly the first Host
1729 * entry. If no Host header is found, then the path is used. The resulting value
1730 * is hashed using the url hash followed by a full avalanche hash and provides a
1731 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1732 * high-traffic sites without having to store whole paths.
1733 * this differs from the base32 functions in that it includes the url parameters
1734 * as well as the path
1735 */
1736static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1737{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001738 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001739 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1740 struct http_hdr_ctx ctx;
1741 struct htx_sl *sl;
1742 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001743 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001744
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001745 if (!htx)
1746 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001747
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001748 ctx.blk = NULL;
1749 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1750 /* OK we have the header value in ctx.value */
1751 while (ctx.value.len--)
1752 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001753 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001754
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001755 /* now retrieve the path */
1756 sl = http_get_stline(htx);
1757 path = http_get_path(htx_sl_req_uri(sl));
1758 while (path.len > 0 && *(path.ptr) != '?') {
1759 path.ptr++;
1760 path.len--;
1761 }
1762 if (path.len && *(path.ptr) == '/') {
1763 while (path.len--)
1764 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001765 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001766
Willy Tarreau79e57332018-10-02 16:01:16 +02001767 hash = full_hash(hash);
1768
1769 smp->data.type = SMP_T_SINT;
1770 smp->data.u.sint = hash;
1771 smp->flags = SMP_F_VOL_1ST;
1772 return 1;
1773}
1774
1775/* This concatenates the source address with the 32-bit hash of the Host and
1776 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1777 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1778 * on the source address length. The URL hash is stored before the address so
1779 * that in environments where IPv6 is insignificant, truncating the output to
1780 * 8 bytes would still work.
1781 */
1782static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1783{
1784 struct buffer *temp;
1785 struct connection *cli_conn = objt_conn(smp->sess->origin);
1786
1787 if (!cli_conn)
1788 return 0;
1789
1790 if (!smp_fetch_url32(args, smp, kw, private))
1791 return 0;
1792
1793 temp = get_trash_chunk();
1794 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1795 temp->data += sizeof(unsigned int);
1796
1797 switch (cli_conn->addr.from.ss_family) {
1798 case AF_INET:
1799 memcpy(temp->area + temp->data,
1800 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1801 4);
1802 temp->data += 4;
1803 break;
1804 case AF_INET6:
1805 memcpy(temp->area + temp->data,
1806 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1807 16);
1808 temp->data += 16;
1809 break;
1810 default:
1811 return 0;
1812 }
1813
1814 smp->data.u.str = *temp;
1815 smp->data.type = SMP_T_BIN;
1816 return 1;
1817}
1818
1819/************************************************************************/
1820/* Other utility functions */
1821/************************************************************************/
1822
1823/* This function is used to validate the arguments passed to any "hdr" fetch
1824 * keyword. These keywords support an optional positive or negative occurrence
1825 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
1826 * is assumed that the types are already the correct ones. Returns 0 on error,
1827 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
1828 * error message in case of error, that the caller is responsible for freeing.
1829 * The initial location must either be freeable or NULL.
1830 * Note: this function's pointer is checked from Lua.
1831 */
1832int val_hdr(struct arg *arg, char **err_msg)
1833{
1834 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
1835 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
1836 return 0;
1837 }
1838 return 1;
1839}
1840
1841/************************************************************************/
1842/* All supported sample fetch keywords must be declared here. */
1843/************************************************************************/
1844
1845/* Note: must not be declared <const> as its list will be overwritten */
1846static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
1847 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1848 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
1849 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
1850
1851 /* capture are allocated and are permanent in the stream */
1852 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
1853
1854 /* retrieve these captures from the HTTP logs */
1855 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1856 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1857 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1858
1859 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
1860 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1861
1862 /* cookie is valid in both directions (eg: for "stick ...") but cook*
1863 * are only here to match the ACL's name, are request-only and are used
1864 * for ACL compatibility only.
1865 */
1866 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001867 { "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 +02001868 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1869 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1870
1871 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
1872 * only here to match the ACL's name, are request-only and are used for
1873 * ACL compatibility only.
1874 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001875 { "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 +02001876 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1877 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
1878 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
1879
1880 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
1881 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
1882 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
1883 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
1884 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1885 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1886
1887 /* HTTP protocol on the request path */
1888 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
1889 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
1890
1891 /* HTTP version on the request path */
1892 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1893 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1894
1895 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
1896 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
1897 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
1898 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
1899
1900 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
1901 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
1902
1903 /* HTTP version on the response path */
1904 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
1905 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
1906
1907 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
1908 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
1909 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1910 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1911
1912 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
1913 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1914 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
1915 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1916 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
1917 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
1918 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
1919
1920 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
1921 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
1922 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1923 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1924
1925 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
1926 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1927 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
1928 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1929 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
1930 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
1931 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
1932
1933 /* scook is valid only on the response and is used for ACL compatibility */
1934 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
1935 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1936 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1937 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
1938
1939 /* shdr is valid only on the response and is used for ACL compatibility */
1940 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
1941 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1942 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
1943 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
1944
1945 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
1946 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
1947 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1948 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
1949 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
1950 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
1951 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
1952 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
1953 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
1954 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1955 { /* END */ },
1956}};
1957
Willy Tarreau0108d902018-11-25 19:14:37 +01001958INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02001959
1960/*
1961 * Local variables:
1962 * c-indent-level: 8
1963 * c-basic-offset: 8
1964 * End:
1965 */