blob: c2dd9b5f136c29f8cccb80a2ed5587250b66593d [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>
36#include <proto/http_fetch.h>
Christopher Fauletef453ed2018-10-24 21:39:27 +020037#include <proto/http_htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020038#include <proto/log.h>
39#include <proto/obj_type.h>
40#include <proto/proto_http.h>
41#include <proto/sample.h>
42#include <proto/stream.h>
43
44
45/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Fauletef453ed2018-10-24 21:39:27 +020046static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
47
Christopher Faulet89dc4992019-04-17 12:02:59 +020048#define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL)
49#define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL)
Willy Tarreau79e57332018-10-02 16:01:16 +020050
51/*
52 * Returns the data from Authorization header. Function may be called more
53 * than once so data is stored in txn->auth_data. When no header is found
54 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
55 * searching again for something we are unable to find anyway. However, if
56 * the result if valid, the cache is not reused because we would risk to
57 * have the credentials overwritten by another stream in parallel.
58 */
59
Christopher Fauletcd761952019-07-15 13:58:29 +020060static int get_http_auth(struct sample *smp, struct htx *htx)
Willy Tarreau79e57332018-10-02 16:01:16 +020061{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020062 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020063 struct http_txn *txn = s->txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020064 struct http_hdr_ctx ctx = { .blk = NULL };
65 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020066 struct buffer auth_method;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020067 char *p;
Willy Tarreau79e57332018-10-02 16:01:16 +020068 int len;
69
70#ifdef DEBUG_AUTH
71 printf("Auth for stream %p: %d\n", s, txn->auth.method);
72#endif
Willy Tarreau79e57332018-10-02 16:01:16 +020073 if (txn->auth.method == HTTP_AUTH_WRONG)
74 return 0;
75
76 txn->auth.method = HTTP_AUTH_WRONG;
77
Christopher Faulet6d1dd462019-07-15 14:36:03 +020078 if (txn->flags & TX_USE_PX_CONN)
79 hdr = ist("Proxy-Authorization");
80 else
81 hdr = ist("Authorization");
Willy Tarreau79e57332018-10-02 16:01:16 +020082
Christopher Faulet6d1dd462019-07-15 14:36:03 +020083 ctx.blk = NULL;
84 if (!http_find_header(htx, hdr, &ctx, 0))
85 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +020086
Christopher Faulet6d1dd462019-07-15 14:36:03 +020087 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
88 len = p - ctx.value.ptr;
89 if (!p || len <= 0)
90 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +020091
Christopher Faulet6d1dd462019-07-15 14:36:03 +020092 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
93 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +020094
Christopher Faulet6d1dd462019-07-15 14:36:03 +020095 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +020096
97 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
98 struct buffer *http_auth = get_trash_chunk();
99
100 len = base64dec(txn->auth.method_data.area,
101 txn->auth.method_data.data,
102 http_auth->area, global.tune.bufsize - 1);
103
104 if (len < 0)
105 return 0;
106
107
108 http_auth->area[len] = '\0';
109
110 p = strchr(http_auth->area, ':');
111
112 if (!p)
113 return 0;
114
115 txn->auth.user = http_auth->area;
116 *p = '\0';
117 txn->auth.pass = p+1;
118
119 txn->auth.method = HTTP_AUTH_BASIC;
120 return 1;
121 }
122
123 return 0;
124}
125
126/* This function ensures that the prerequisites for an L7 fetch are ready,
127 * which means that a request or response is ready. If some data is missing,
128 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200129 * to extract data from L7. If <vol> is non-null during a prefetch, another
130 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200131 *
132 * The function returns :
133 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
134 * decide whether or not an HTTP message is present ;
135 * NULL if the requested data cannot be fetched or if it is certain that
136 * we'll never have any HTTP message there ;
137 * The HTX message if ready
138 */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200139struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200140{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200141 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200142 struct http_txn *txn = NULL;
143 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200144 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100145 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200146
147 /* Note: it is possible that <s> is NULL when called before stream
148 * initialization (eg: tcp-request connection), so this function is the
149 * one responsible for guarding against this case for all HTTP users.
150 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200151 if (!s || !chn)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200152 return NULL;
153
154 if (!s->txn) {
155 if (unlikely(!http_alloc_txn(s)))
156 return NULL; /* not enough memory */
157 http_init_txn(s);
158 txn = s->txn;
159 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200160 txn = s->txn;
161 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
162 smp->data.type = SMP_T_BOOL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200163
Christopher Fauleteca88542019-04-03 10:12:42 +0200164 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200165 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200166
Christopher Faulet89dc4992019-04-17 12:02:59 +0200167 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
168 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200169
Christopher Faulet89dc4992019-04-17 12:02:59 +0200170 if (msg->msg_state < HTTP_MSG_BODY) {
171 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200172 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200173 /* Parsing is done by the mux, just wait */
174 smp->flags |= SMP_F_MAY_CHANGE;
175 return NULL;
176 }
177 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200178 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200179 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200180 /* The start-line was already forwarded, it is too late to fetch anything */
181 return NULL;
182 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200183 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200184 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200185 struct buffer *buf;
186 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200187 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200188 union h1_sl h1sl;
189 unsigned int flags = HTX_FL_NONE;
190 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200191
Christopher Faulet89dc4992019-04-17 12:02:59 +0200192 /* no HTTP fetch on the response in TCP mode */
193 if (chn->flags & CF_ISRESP)
194 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200195
Christopher Faulet89dc4992019-04-17 12:02:59 +0200196 /* Now we are working on the request only */
197 buf = &chn->buf;
198 if (b_head(buf) + b_data(buf) > b_wrap(buf))
199 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200200
Christopher Faulet89dc4992019-04-17 12:02:59 +0200201 h1m_init_req(&h1m);
202 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
203 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
204 if (ret <= 0) {
205 /* Invalid or too big*/
206 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200207 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100208
Christopher Faulet89dc4992019-04-17 12:02:59 +0200209 /* wait for a full request */
210 smp->flags |= SMP_F_MAY_CHANGE;
211 return NULL;
212 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100213
Christopher Faulet89dc4992019-04-17 12:02:59 +0200214 /* OK we just got a valid HTTP mesage. We have to convert it
215 * into an HTX message.
216 */
217 if (unlikely(h1sl.rq.v.len == 0)) {
218 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
219 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200220 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200221 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200222 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200223
224 /* Set HTX start-line flags */
225 if (h1m.flags & H1_MF_VER_11)
226 flags |= HTX_SL_F_VER_11;
227 if (h1m.flags & H1_MF_XFER_ENC)
228 flags |= HTX_SL_F_XFER_ENC;
229 flags |= HTX_SL_F_XFER_LEN;
230 if (h1m.flags & H1_MF_CHNK)
231 flags |= HTX_SL_F_CHNK;
232 else if (h1m.flags & H1_MF_CLEN)
233 flags |= HTX_SL_F_CLEN;
234
235 htx = htx_from_buf(get_trash_chunk());
236 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
237 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200238 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200239 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200240 }
241
242 /* OK we just got a valid HTTP message. If not already done by
243 * HTTP analyzers, we have some minor preparation to perform so
244 * that further checks can rely on HTTP tests.
245 */
246 if (sl && msg->msg_state < HTTP_MSG_BODY) {
247 if (!(chn->flags & CF_ISRESP)) {
248 txn->meth = sl->info.req.meth;
249 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
250 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200251 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200252 else
253 txn->status = sl->info.res.status;
254 if (sl->flags & HTX_SL_F_VER_11)
255 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200256 }
257
258 /* everything's OK */
259 smp->data.u.sint = 1;
260 return htx;
261}
262
Willy Tarreau79e57332018-10-02 16:01:16 +0200263/* This function fetches the method of current HTTP request and stores
264 * it in the global pattern struct as a chunk. There are two possibilities :
265 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
266 * in <len> and <ptr> is NULL ;
267 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
268 * <len> to its length.
269 * This is intended to be used with pat_match_meth() only.
270 */
271static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
272{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200273 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200274 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +0200275 struct http_txn *txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200276 int meth;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200277
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200278 if (!htx)
279 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200280
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200281 txn = smp->strm->txn;
282 meth = txn->meth;
283 smp->data.type = SMP_T_METH;
284 smp->data.u.meth.meth = meth;
285 if (meth == HTTP_METH_OTHER) {
286 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200287
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200288 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
289 /* ensure the indexes are not affected */
290 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200291 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200292 sl = http_get_stline(htx);
293 smp->flags |= SMP_F_CONST;
294 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
295 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200296 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200297 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200298 return 1;
299}
300
301static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
302{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200303 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200304 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
305 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200306 char *ptr;
307 int len;
308
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200309 if (!htx)
310 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200311
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200312 sl = http_get_stline(htx);
313 len = HTX_SL_REQ_VLEN(sl);
314 ptr = HTX_SL_REQ_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200315
316 while ((len-- > 0) && (*ptr++ != '/'));
317 if (len <= 0)
318 return 0;
319
320 smp->data.type = SMP_T_STR;
321 smp->data.u.str.area = ptr;
322 smp->data.u.str.data = len;
323
324 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
325 return 1;
326}
327
328static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
329{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200330 struct channel *chn = SMP_RES_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200331 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
332 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200333 char *ptr;
334 int len;
335
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200336 if (!htx)
337 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200338
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200339 sl = http_get_stline(htx);
340 len = HTX_SL_RES_VLEN(sl);
341 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200342
343 while ((len-- > 0) && (*ptr++ != '/'));
344 if (len <= 0)
345 return 0;
346
347 smp->data.type = SMP_T_STR;
348 smp->data.u.str.area = ptr;
349 smp->data.u.str.data = len;
350
351 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
352 return 1;
353}
354
355/* 3. Check on Status Code. We manipulate integers here. */
356static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
357{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200358 struct channel *chn = SMP_RES_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200359 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
360 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200361 char *ptr;
362 int len;
363
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200364 if (!htx)
365 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200366
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200367 sl = http_get_stline(htx);
368 len = HTX_SL_RES_CLEN(sl);
369 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200370
371 smp->data.type = SMP_T_SINT;
372 smp->data.u.sint = __strl2ui(ptr, len);
373 smp->flags = SMP_F_VOL_1ST;
374 return 1;
375}
376
377static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
378{
379 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
380 return 0;
381
382 if (!smp->strm->unique_id) {
383 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
384 return 0;
385 smp->strm->unique_id[0] = '\0';
386 }
387 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
388 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
389
390 smp->data.type = SMP_T_STR;
391 smp->data.u.str.area = smp->strm->unique_id;
392 smp->flags = SMP_F_CONST;
393 return 1;
394}
395
396/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800397 * empty line which separes headers from the body. This is useful
398 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200399 */
400static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
401{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200402 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200403 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
404 struct buffer *temp;
405 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200406
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200407 if (!htx)
408 return 0;
409 temp = get_trash_chunk();
410 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
411 struct htx_blk *blk = htx_get_blk(htx, pos);
412 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200413
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200414 if (type == HTX_BLK_HDR) {
415 struct ist n = htx_get_blk_name(htx, blk);
416 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200417
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200418 if (!htx_hdr_to_h1(n, v, temp))
419 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200420 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200421 else if (type == HTX_BLK_EOH) {
422 if (!chunk_memcat(temp, "\r\n", 2))
423 return 0;
424 break;
425 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200426 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200427 smp->data.type = SMP_T_STR;
428 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200429 return 1;
430}
431
432/* Returns the header request in a length/value encoded format.
433 * This is useful for exchanges with the SPOE.
434 *
435 * A "length value" is a multibyte code encoding numbers. It uses the
436 * SPOE format. The encoding is the following:
437 *
438 * Each couple "header name" / "header value" is composed
439 * like this:
440 * "length value" "header name bytes"
441 * "length value" "header value bytes"
442 * When the last header is reached, the header name and the header
443 * value are empty. Their length are 0
444 */
445static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
446{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200447 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200448 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200449 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200450 char *p, *end;
451 int32_t pos;
452 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200453
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200454 if (!htx)
455 return 0;
456 temp = get_trash_chunk();
457 p = temp->area;
458 end = temp->area + temp->size;
459 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
460 struct htx_blk *blk = htx_get_blk(htx, pos);
461 enum htx_blk_type type = htx_get_blk_type(blk);
462 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200463
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200464 if (type == HTX_BLK_HDR) {
465 n = htx_get_blk_name(htx,blk);
466 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200467
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200468 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200469 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200470 if (ret == -1)
471 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200472 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200473 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200474 memcpy(p, n.ptr, n.len);
475 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200476
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200477 /* encode the header value. */
478 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200479 if (ret == -1)
480 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200481 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200482 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200483 memcpy(p, v.ptr, v.len);
484 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200485
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200486 }
487 else if (type == HTX_BLK_EOH) {
488 /* encode the end of the header list with empty
489 * header name and header value.
490 */
491 ret = encode_varint(0, &p, end);
492 if (ret == -1)
493 return 0;
494 ret = encode_varint(0, &p, end);
495 if (ret == -1)
496 return 0;
497 break;
498 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200499 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200500
501 /* Initialise sample data which will be filled. */
502 smp->data.type = SMP_T_BIN;
503 smp->data.u.str.area = temp->area;
504 smp->data.u.str.data = p - temp->area;
505 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200506 return 1;
507}
508
509/* returns the longest available part of the body. This requires that the body
510 * has been waited for using http-buffer-request.
511 */
512static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
513{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200514 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200515 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200516 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200517 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200518
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200519 if (!htx)
520 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200521
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200522 temp = get_trash_chunk();
523 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
524 struct htx_blk *blk = htx_get_blk(htx, pos);
525 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200526
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200527 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
528 break;
529 if (type == HTX_BLK_DATA) {
530 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
531 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200532 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200533 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200534
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200535 smp->data.type = SMP_T_BIN;
536 smp->data.u.str = *temp;
537 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200538 return 1;
539}
540
541
542/* returns the available length of the body. This requires that the body
543 * has been waited for using http-buffer-request.
544 */
545static int smp_fetch_body_len(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);
549 int32_t pos;
550 unsigned long long len = 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100551
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200552 if (!htx)
553 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100554
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200555 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
556 struct htx_blk *blk = htx_get_blk(htx, pos);
557 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100558
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200559 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
560 break;
561 if (type == HTX_BLK_DATA)
562 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200563 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200564
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200565 smp->data.type = SMP_T_SINT;
566 smp->data.u.sint = len;
567 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200568 return 1;
569}
570
571
572/* returns the advertised length of the body, or the advertised size of the
573 * chunks available in the buffer. This requires that the body has been waited
574 * for using http-buffer-request.
575 */
576static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
577{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200578 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200579 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
580 int32_t pos;
581 unsigned long long len = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200582
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200583 if (!htx)
584 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100585
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200586 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
587 struct htx_blk *blk = htx_get_blk(htx, pos);
588 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100589
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200590 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
591 break;
592 if (type == HTX_BLK_DATA)
593 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200594 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200595 if (htx->extra != ULLONG_MAX)
596 len += htx->extra;
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/* 4. Check on URL/URI. A pointer to the URI is stored. */
606static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
607{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200608 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200609 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
610 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200611
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200612 if (!htx)
613 return 0;
614 sl = http_get_stline(htx);
615 smp->data.type = SMP_T_STR;
616 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
617 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
618 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200619 return 1;
620}
621
622static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
623{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200624 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200625 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
626 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200627 struct sockaddr_storage addr;
628
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200629 if (!htx)
630 return 0;
631 sl = http_get_stline(htx);
632 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +0200633
Willy Tarreau79e57332018-10-02 16:01:16 +0200634 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
635 return 0;
636
637 smp->data.type = SMP_T_IPV4;
638 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
639 smp->flags = 0;
640 return 1;
641}
642
643static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
644{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200645 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200646 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
647 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200648 struct sockaddr_storage addr;
649
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200650 if (!htx)
651 return 0;
652 sl = http_get_stline(htx);
653 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200654
Willy Tarreau79e57332018-10-02 16:01:16 +0200655 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
656 return 0;
657
658 smp->data.type = SMP_T_SINT;
659 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
660 smp->flags = 0;
661 return 1;
662}
663
664/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
665 * Accepts an optional argument of type string containing the header field name,
666 * and an optional argument of type signed or unsigned integer to request an
667 * explicit occurrence of the header. Note that in the event of a missing name,
668 * headers are considered from the first one. It does not stop on commas and
669 * returns full lines instead (useful for User-Agent or Date for example).
670 */
671static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
672{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200673 /* possible keywords: req.fhdr, res.fhdr */
674 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200675 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
676 struct http_hdr_ctx *ctx = smp->ctx.a[0];
677 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200678 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200679
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200680 if (!ctx) {
681 /* first call */
682 ctx = &static_http_hdr_ctx;
683 ctx->blk = NULL;
684 smp->ctx.a[0] = ctx;
685 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200686
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200687 if (args) {
688 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200689 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200690 name.ptr = args[0].data.str.area;
691 name.len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +0200692
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200693 if (args[1].type == ARGT_SINT)
694 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200695 }
696
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200697 if (!htx)
698 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200699
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200700 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
701 /* search for header from the beginning */
702 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200703
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200704 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
705 /* no explicit occurrence and single fetch => last header by default */
706 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200707
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200708 if (!occ)
709 /* prepare to report multiple occurrences for ACL fetches */
710 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200711
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200712 smp->data.type = SMP_T_STR;
713 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
714 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
715 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200716 smp->flags &= ~SMP_F_NOT_LAST;
717 return 0;
718}
719
720/* 6. Check on HTTP header count. The number of occurrences is returned.
721 * Accepts exactly 1 argument of type string. It does not stop on commas and
722 * returns full lines instead (useful for User-Agent or Date for example).
723 */
724static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
725{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200726 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
727 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200728 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
729 struct http_hdr_ctx ctx;
730 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200731 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200732
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200733 if (!htx)
734 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200735
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200736 if (args && args->type == ARGT_STR) {
737 name.ptr = args->data.str.area;
738 name.len = args->data.str.data;
739 } else {
740 name.ptr = NULL;
741 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200742 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200743
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200744 ctx.blk = NULL;
745 cnt = 0;
746 while (http_find_header(htx, name, &ctx, 1))
747 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200748 smp->data.type = SMP_T_SINT;
749 smp->data.u.sint = cnt;
750 smp->flags = SMP_F_VOL_HDR;
751 return 1;
752}
753
754static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
755{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200756 /* possible keywords: req.hdr_names, res.hdr_names */
757 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200758 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200759 struct buffer *temp;
760 char del = ',';
761
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200762 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200763
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200764 if (!htx)
765 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200766
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200767 if (args && args->type == ARGT_STR)
768 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200769
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200770 temp = get_trash_chunk();
771 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
772 struct htx_blk *blk = htx_get_blk(htx, pos);
773 enum htx_blk_type type = htx_get_blk_type(blk);
774 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200775
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200776 if (type == HTX_BLK_EOH)
777 break;
778 if (type != HTX_BLK_HDR)
779 continue;
780 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200781
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200782 if (temp->data)
783 temp->area[temp->data++] = del;
784 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200785 }
786
787 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200788 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200789 smp->flags = SMP_F_VOL_HDR;
790 return 1;
791}
792
793/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
794 * Accepts an optional argument of type string containing the header field name,
795 * and an optional argument of type signed or unsigned integer to request an
796 * explicit occurrence of the header. Note that in the event of a missing name,
797 * headers are considered from the first one.
798 */
799static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
800{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200801 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
802 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200803 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
804 struct http_hdr_ctx *ctx = smp->ctx.a[0];
805 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200806 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200807
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200808 if (!ctx) {
809 /* first call */
810 ctx = &static_http_hdr_ctx;
811 ctx->blk = NULL;
812 smp->ctx.a[0] = ctx;
813 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200814
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200815 if (args) {
816 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200817 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200818 name.ptr = args[0].data.str.area;
819 name.len = args[0].data.str.data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200820
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200821 if (args[1].type == ARGT_SINT)
822 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200823 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200824
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200825 if (!htx)
826 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200827
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200828 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
829 /* search for header from the beginning */
830 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200831
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200832 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
833 /* no explicit occurrence and single fetch => last header by default */
834 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200835
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200836 if (!occ)
837 /* prepare to report multiple occurrences for ACL fetches */
838 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200839
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200840 smp->data.type = SMP_T_STR;
841 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
842 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
843 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200844
845 smp->flags &= ~SMP_F_NOT_LAST;
846 return 0;
847}
848
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200849/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
850 * the right channel. So instead of duplicating the code, we just change the
851 * keyword and then fallback on smp_fetch_hdr().
852 */
853static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
854{
855 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
856 return smp_fetch_hdr(args, smp, kw, private);
857}
858
Willy Tarreau79e57332018-10-02 16:01:16 +0200859/* 6. Check on HTTP header count. The number of occurrences is returned.
860 * Accepts exactly 1 argument of type string.
861 */
862static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
863{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200864 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
865 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200866 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
867 struct http_hdr_ctx ctx;
868 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200869 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200870
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200871 if (!htx)
872 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200873
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200874 if (args && args->type == ARGT_STR) {
875 name.ptr = args->data.str.area;
876 name.len = args->data.str.data;
877 } else {
878 name.ptr = NULL;
879 name.len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200880 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200881
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200882 ctx.blk = NULL;
883 cnt = 0;
884 while (http_find_header(htx, name, &ctx, 0))
885 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200886
887 smp->data.type = SMP_T_SINT;
888 smp->data.u.sint = cnt;
889 smp->flags = SMP_F_VOL_HDR;
890 return 1;
891}
892
893/* Fetch an HTTP header's integer value. The integer value is returned. It
894 * takes a mandatory argument of type string and an optional one of type int
895 * to designate a specific occurrence. It returns an unsigned integer, which
896 * may or may not be appropriate for everything.
897 */
898static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
899{
900 int ret = smp_fetch_hdr(args, smp, kw, private);
901
902 if (ret > 0) {
903 smp->data.type = SMP_T_SINT;
904 smp->data.u.sint = strl2ic(smp->data.u.str.area,
905 smp->data.u.str.data);
906 }
907
908 return ret;
909}
910
911/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
912 * and an optional one of type int to designate a specific occurrence.
913 * It returns an IPv4 or IPv6 address.
914 */
915static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
916{
917 int ret;
918
919 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
920 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
921 smp->data.type = SMP_T_IPV4;
922 break;
923 } else {
924 struct buffer *temp = get_trash_chunk();
925 if (smp->data.u.str.data < temp->size - 1) {
926 memcpy(temp->area, smp->data.u.str.area,
927 smp->data.u.str.data);
928 temp->area[smp->data.u.str.data] = '\0';
929 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
930 smp->data.type = SMP_T_IPV6;
931 break;
932 }
933 }
934 }
935
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200936 /* if the header doesn't match an IP address, fetch next one */
937 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200938 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200939 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200940 return ret;
941}
Willy Tarreau79e57332018-10-02 16:01:16 +0200942
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200943/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
944 * the first '/' after the possible hostname, and ends before the possible '?'.
945 */
946static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
947{
948 struct channel *chn = SMP_REQ_CHN(smp);
949 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
950 struct htx_sl *sl;
951 struct ist path;
952 size_t len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200953
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200954 if (!htx)
955 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200956
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200957 sl = http_get_stline(htx);
958 path = http_get_path(htx_sl_req_uri(sl));
959 if (!path.ptr)
960 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200961
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200962 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
963 ;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200964
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200965 /* OK, we got the '/' ! */
966 smp->data.type = SMP_T_STR;
967 smp->data.u.str.area = path.ptr;
968 smp->data.u.str.data = len;
969 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200970 return 1;
971}
972
973/* This produces a concatenation of the first occurrence of the Host header
974 * followed by the path component if it begins with a slash ('/'). This means
975 * that '*' will not be added, resulting in exactly the first Host entry.
976 * If no Host header is found, then the path is returned as-is. The returned
977 * value is stored in the trash so it does not need to be marked constant.
978 * The returned sample is of type string.
979 */
980static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
981{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200982 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200983 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
984 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200985 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200986 struct http_hdr_ctx ctx;
987 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200988
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200989 if (!htx)
990 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200991
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200992 ctx.blk = NULL;
993 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
994 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200995
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200996 /* OK we have the header value in ctx.value */
997 temp = get_trash_chunk();
998 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200999
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001000 /* now retrieve the path */
1001 sl = http_get_stline(htx);
1002 path = http_get_path(htx_sl_req_uri(sl));
1003 if (path.ptr) {
1004 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001005
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001006 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1007 ;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001008
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001009 if (len && *(path.ptr) == '/')
1010 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001011 }
1012
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001013 smp->data.type = SMP_T_STR;
1014 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001015 smp->flags = SMP_F_VOL_1ST;
1016 return 1;
1017}
1018
1019/* This produces a 32-bit hash of the concatenation of the first occurrence of
1020 * the Host header followed by the path component if it begins with a slash ('/').
1021 * This means that '*' will not be added, resulting in exactly the first Host
1022 * entry. If no Host header is found, then the path is used. The resulting value
1023 * is hashed using the path hash followed by a full avalanche hash and provides a
1024 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1025 * high-traffic sites without having to store whole paths.
1026 */
1027static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1028{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001029 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001030 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1031 struct htx_sl *sl;
1032 struct http_hdr_ctx ctx;
1033 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001034 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001035
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001036 if (!htx)
1037 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001038
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001039 ctx.blk = NULL;
1040 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1041 /* OK we have the header value in ctx.value */
1042 while (ctx.value.len--)
1043 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001044 }
1045
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001046 /* now retrieve the path */
1047 sl = http_get_stline(htx);
1048 path = http_get_path(htx_sl_req_uri(sl));
1049 if (path.ptr) {
1050 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001051
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001052 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1053 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001054
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001055 if (len && *(path.ptr) == '/') {
1056 while (len--)
1057 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001058 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001059 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001060
Willy Tarreau79e57332018-10-02 16:01:16 +02001061 hash = full_hash(hash);
1062
1063 smp->data.type = SMP_T_SINT;
1064 smp->data.u.sint = hash;
1065 smp->flags = SMP_F_VOL_1ST;
1066 return 1;
1067}
1068
1069/* This concatenates the source address with the 32-bit hash of the Host and
1070 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1071 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1072 * on the source address length. The path hash is stored before the address so
1073 * that in environments where IPv6 is insignificant, truncating the output to
1074 * 8 bytes would still work.
1075 */
1076static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1077{
1078 struct buffer *temp;
1079 struct connection *cli_conn = objt_conn(smp->sess->origin);
1080
1081 if (!cli_conn)
1082 return 0;
1083
1084 if (!smp_fetch_base32(args, smp, kw, private))
1085 return 0;
1086
1087 temp = get_trash_chunk();
1088 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1089 temp->data += sizeof(unsigned int);
1090
1091 switch (cli_conn->addr.from.ss_family) {
1092 case AF_INET:
1093 memcpy(temp->area + temp->data,
1094 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1095 4);
1096 temp->data += 4;
1097 break;
1098 case AF_INET6:
1099 memcpy(temp->area + temp->data,
1100 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1101 16);
1102 temp->data += 16;
1103 break;
1104 default:
1105 return 0;
1106 }
1107
1108 smp->data.u.str = *temp;
1109 smp->data.type = SMP_T_BIN;
1110 return 1;
1111}
1112
1113/* Extracts the query string, which comes after the question mark '?'. If no
1114 * question mark is found, nothing is returned. Otherwise it returns a sample
1115 * of type string carrying the whole query string.
1116 */
1117static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1118{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001119 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001120 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1121 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001122 char *ptr, *end;
1123
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001124 if (!htx)
1125 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001126
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001127 sl = http_get_stline(htx);
1128 ptr = HTX_SL_REQ_UPTR(sl);
1129 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001130
1131 /* look up the '?' */
1132 do {
1133 if (ptr == end)
1134 return 0;
1135 } while (*ptr++ != '?');
1136
1137 smp->data.type = SMP_T_STR;
1138 smp->data.u.str.area = ptr;
1139 smp->data.u.str.data = end - ptr;
1140 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1141 return 1;
1142}
1143
1144static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1145{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001146 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001147 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001148
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001149 if (!htx)
1150 return 0;
1151 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001152 smp->data.u.sint = 1;
1153 return 1;
1154}
1155
1156/* return a valid test if the current request is the first one on the connection */
1157static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1158{
1159 smp->data.type = SMP_T_BOOL;
1160 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1161 return 1;
1162}
1163
1164/* Accepts exactly 1 argument of type userlist */
1165static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1166{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001167 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001168 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001169
1170 if (!args || args->type != ARGT_USR)
1171 return 0;
1172
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001173 if (!htx)
1174 return 0;
1175 if (!get_http_auth(smp, htx))
1176 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001177
1178 smp->data.type = SMP_T_BOOL;
1179 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001180 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001181 return 1;
1182}
1183
1184/* Accepts exactly 1 argument of type userlist */
1185static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1186{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001187 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001188 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001189
Willy Tarreau79e57332018-10-02 16:01:16 +02001190 if (!args || args->type != ARGT_USR)
1191 return 0;
1192
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001193 if (!htx)
1194 return 0;
1195 if (!get_http_auth(smp, htx))
1196 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001197
Willy Tarreau79e57332018-10-02 16:01:16 +02001198 /* if the user does not belong to the userlist or has a wrong password,
1199 * report that it unconditionally does not match. Otherwise we return
1200 * a string containing the username.
1201 */
1202 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1203 smp->strm->txn->auth.pass))
1204 return 0;
1205
1206 /* pat_match_auth() will need the user list */
1207 smp->ctx.a[0] = args->data.usr;
1208
1209 smp->data.type = SMP_T_STR;
1210 smp->flags = SMP_F_CONST;
1211 smp->data.u.str.area = smp->strm->txn->auth.user;
1212 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1213
1214 return 1;
1215}
1216
1217/* Fetch a captured HTTP request header. The index is the position of
1218 * the "capture" option in the configuration file
1219 */
1220static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1221{
1222 struct proxy *fe = strm_fe(smp->strm);
1223 int idx;
1224
1225 if (!args || args->type != ARGT_SINT)
1226 return 0;
1227
1228 idx = args->data.sint;
1229
1230 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1231 return 0;
1232
1233 smp->data.type = SMP_T_STR;
1234 smp->flags |= SMP_F_CONST;
1235 smp->data.u.str.area = smp->strm->req_cap[idx];
1236 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1237
1238 return 1;
1239}
1240
1241/* Fetch a captured HTTP response header. The index is the position of
1242 * the "capture" option in the configuration file
1243 */
1244static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1245{
1246 struct proxy *fe = strm_fe(smp->strm);
1247 int idx;
1248
1249 if (!args || args->type != ARGT_SINT)
1250 return 0;
1251
1252 idx = args->data.sint;
1253
1254 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1255 return 0;
1256
1257 smp->data.type = SMP_T_STR;
1258 smp->flags |= SMP_F_CONST;
1259 smp->data.u.str.area = smp->strm->res_cap[idx];
1260 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1261
1262 return 1;
1263}
1264
1265/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1266static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1267{
1268 struct buffer *temp;
1269 struct http_txn *txn = smp->strm->txn;
1270 char *ptr;
1271
1272 if (!txn || !txn->uri)
1273 return 0;
1274
1275 ptr = txn->uri;
1276
1277 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1278 ptr++;
1279
1280 temp = get_trash_chunk();
1281 temp->area = txn->uri;
1282 temp->data = ptr - txn->uri;
1283 smp->data.u.str = *temp;
1284 smp->data.type = SMP_T_STR;
1285 smp->flags = SMP_F_CONST;
1286
1287 return 1;
1288
1289}
1290
1291/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1292static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1293{
1294 struct http_txn *txn = smp->strm->txn;
1295 struct ist path;
1296 const char *ptr;
1297
1298 if (!txn || !txn->uri)
1299 return 0;
1300
1301 ptr = txn->uri;
1302
1303 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1304 ptr++;
1305
1306 if (!*ptr)
1307 return 0;
1308
Christopher Faulet78337bb2018-11-15 14:35:18 +01001309 /* skip the first space and find space after URI */
1310 path = ist2(++ptr, 0);
1311 while (*ptr != ' ' && *ptr != '\0')
1312 ptr++;
1313 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001314
Christopher Faulet78337bb2018-11-15 14:35:18 +01001315 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02001316 if (!path.ptr)
1317 return 0;
1318
1319 smp->data.u.str.area = path.ptr;
1320 smp->data.u.str.data = path.len;
1321 smp->data.type = SMP_T_STR;
1322 smp->flags = SMP_F_CONST;
1323
1324 return 1;
1325}
1326
1327/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1328 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1329 */
1330static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1331{
1332 struct http_txn *txn = smp->strm->txn;
1333
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001334 if (!txn || txn->req.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001335 return 0;
1336
1337 if (txn->req.flags & HTTP_MSGF_VER_11)
1338 smp->data.u.str.area = "HTTP/1.1";
1339 else
1340 smp->data.u.str.area = "HTTP/1.0";
1341
1342 smp->data.u.str.data = 8;
1343 smp->data.type = SMP_T_STR;
1344 smp->flags = SMP_F_CONST;
1345 return 1;
1346
1347}
1348
1349/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1350 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1351 */
1352static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1353{
1354 struct http_txn *txn = smp->strm->txn;
1355
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001356 if (!txn || txn->rsp.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001357 return 0;
1358
1359 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1360 smp->data.u.str.area = "HTTP/1.1";
1361 else
1362 smp->data.u.str.area = "HTTP/1.0";
1363
1364 smp->data.u.str.data = 8;
1365 smp->data.type = SMP_T_STR;
1366 smp->flags = SMP_F_CONST;
1367 return 1;
1368
1369}
1370
1371/* Iterate over all cookies present in a message. The context is stored in
1372 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1373 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1374 * the direction, multiple cookies may be parsed on the same line or not.
1375 * The cookie name is in args and the name length in args->data.str.len.
1376 * Accepts exactly 1 argument of type string. If the input options indicate
1377 * that no iterating is desired, then only last value is fetched if any.
1378 * The returned sample is of type CSTR. Can be used to parse cookies in other
1379 * files.
1380 */
1381static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1382{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001383 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1384 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001385 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1386 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1387 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001388 int occ = 0;
1389 int found = 0;
1390
1391 if (!args || args->type != ARGT_STR)
1392 return 0;
1393
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001394 if (!ctx) {
1395 /* first call */
1396 ctx = &static_http_hdr_ctx;
1397 ctx->blk = NULL;
1398 smp->ctx.a[2] = ctx;
1399 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001400
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001401 if (!htx)
1402 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001403
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001404 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001405
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001406 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1407 /* no explicit occurrence and single fetch => last cookie by default */
1408 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001409
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001410 /* OK so basically here, either we want only one value and it's the
1411 * last one, or we want to iterate over all of them and we fetch the
1412 * next one.
1413 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001414
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001415 if (!(smp->flags & SMP_F_NOT_LAST)) {
1416 /* search for the header from the beginning, we must first initialize
1417 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001418 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001419 smp->ctx.a[0] = NULL;
1420 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001421 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001422
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001423 smp->flags |= SMP_F_VOL_HDR;
1424 while (1) {
1425 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1426 if (!smp->ctx.a[0]) {
1427 if (!http_find_header(htx, hdr, ctx, 0))
1428 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001429
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001430 if (ctx->value.len < args->data.str.data + 1)
1431 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001432
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001433 smp->ctx.a[0] = ctx->value.ptr;
1434 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001435 }
1436
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001437 smp->data.type = SMP_T_STR;
1438 smp->flags |= SMP_F_CONST;
1439 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
1440 args->data.str.area, args->data.str.data,
1441 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1442 &smp->data.u.str.area,
1443 &smp->data.u.str.data);
1444 if (smp->ctx.a[0]) {
1445 found = 1;
1446 if (occ >= 0) {
1447 /* one value was returned into smp->data.u.str.{str,len} */
1448 smp->flags |= SMP_F_NOT_LAST;
1449 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001450 }
1451 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001452 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001453 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001454
Willy Tarreau79e57332018-10-02 16:01:16 +02001455 /* all cookie headers and values were scanned. If we're looking for the
1456 * last occurrence, we may return it now.
1457 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001458 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001459 smp->flags &= ~SMP_F_NOT_LAST;
1460 return found;
1461}
1462
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001463/* Same than smp_fetch_cookie() but only relies on the sample direction to
1464 * choose the right channel. So instead of duplicating the code, we just change
1465 * the keyword and then fallback on smp_fetch_cookie().
1466 */
1467static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1468{
1469 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1470 return smp_fetch_cookie(args, smp, kw, private);
1471}
1472
Willy Tarreau79e57332018-10-02 16:01:16 +02001473/* Iterate over all cookies present in a request to count how many occurrences
1474 * match the name in args and args->data.str.len. If <multi> is non-null, then
1475 * multiple cookies may be parsed on the same line. The returned sample is of
1476 * type UINT. Accepts exactly 1 argument of type string.
1477 */
1478static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1479{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001480 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1481 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001482 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1483 struct http_hdr_ctx ctx;
1484 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001485 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001486 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001487
1488 if (!args || args->type != ARGT_STR)
1489 return 0;
1490
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001491 if (!htx)
1492 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001493
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001494 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001495
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001496 val_end = val_beg = NULL;
1497 ctx.blk = NULL;
1498 cnt = 0;
1499 while (1) {
1500 /* Note: val_beg == NULL every time we need to fetch a new header */
1501 if (!val_beg) {
1502 if (!http_find_header(htx, hdr, &ctx, 0))
1503 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001504
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001505 if (ctx.value.len < args->data.str.data + 1)
1506 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001507
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001508 val_beg = ctx.value.ptr;
1509 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001510 }
1511
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001512 smp->data.type = SMP_T_STR;
1513 smp->flags |= SMP_F_CONST;
1514 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
1515 args->data.str.area, args->data.str.data,
1516 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1517 &smp->data.u.str.area,
1518 &smp->data.u.str.data))) {
1519 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001520 }
1521 }
1522
1523 smp->data.type = SMP_T_SINT;
1524 smp->data.u.sint = cnt;
1525 smp->flags |= SMP_F_VOL_HDR;
1526 return 1;
1527}
1528
1529/* Fetch an cookie's integer value. The integer value is returned. It
1530 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1531 */
1532static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1533{
1534 int ret = smp_fetch_cookie(args, smp, kw, private);
1535
1536 if (ret > 0) {
1537 smp->data.type = SMP_T_SINT;
1538 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1539 smp->data.u.str.data);
1540 }
1541
1542 return ret;
1543}
1544
1545/************************************************************************/
1546/* The code below is dedicated to sample fetches */
1547/************************************************************************/
1548
1549/* This scans a URL-encoded query string. It takes an optionally wrapping
1550 * string whose first contigous chunk has its beginning in ctx->a[0] and end
1551 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1552 * pointers are updated for next iteration before leaving.
1553 */
1554static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1555{
1556 const char *vstart, *vend;
1557 struct buffer *temp;
1558 const char **chunks = (const char **)smp->ctx.a;
1559
1560 if (!http_find_next_url_param(chunks, name, name_len,
1561 &vstart, &vend, delim))
1562 return 0;
1563
1564 /* Create sample. If the value is contiguous, return the pointer as CONST,
1565 * if the value is wrapped, copy-it in a buffer.
1566 */
1567 smp->data.type = SMP_T_STR;
1568 if (chunks[2] &&
1569 vstart >= chunks[0] && vstart <= chunks[1] &&
1570 vend >= chunks[2] && vend <= chunks[3]) {
1571 /* Wrapped case. */
1572 temp = get_trash_chunk();
1573 memcpy(temp->area, vstart, chunks[1] - vstart);
1574 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1575 vend - chunks[2]);
1576 smp->data.u.str.area = temp->area;
1577 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1578 } else {
1579 /* Contiguous case. */
1580 smp->data.u.str.area = (char *)vstart;
1581 smp->data.u.str.data = vend - vstart;
1582 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1583 }
1584
1585 /* Update context, check wrapping. */
1586 chunks[0] = vend;
1587 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1588 chunks[1] = chunks[3];
1589 chunks[2] = NULL;
1590 }
1591
1592 if (chunks[0] < chunks[1])
1593 smp->flags |= SMP_F_NOT_LAST;
1594
1595 return 1;
1596}
1597
1598/* This function iterates over each parameter of the query string. It uses
1599 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1600 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1601 * An optional parameter name is passed in args[0], otherwise any parameter is
1602 * considered. It supports an optional delimiter argument for the beginning of
1603 * the string in args[1], which defaults to "?".
1604 */
1605static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1606{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001607 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001608 char delim = '?';
1609 const char *name;
1610 int name_len;
1611
1612 if (!args ||
1613 (args[0].type && args[0].type != ARGT_STR) ||
1614 (args[1].type && args[1].type != ARGT_STR))
1615 return 0;
1616
1617 name = "";
1618 name_len = 0;
1619 if (args->type == ARGT_STR) {
1620 name = args->data.str.area;
1621 name_len = args->data.str.data;
1622 }
1623
1624 if (args[1].type)
1625 delim = *args[1].data.str.area;
1626
1627 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001628 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1629 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001630
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001631 if (!htx)
1632 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001633
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001634 sl = http_get_stline(htx);
1635 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1636 if (!smp->ctx.a[0])
1637 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001638
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001639 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001640
1641 /* Assume that the context is filled with NULL pointer
1642 * before the first call.
1643 * smp->ctx.a[2] = NULL;
1644 * smp->ctx.a[3] = NULL;
1645 */
1646 }
1647
1648 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1649}
1650
1651/* This function iterates over each parameter of the body. This requires
1652 * that the body has been waited for using http-buffer-request. It uses
1653 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
1654 * contigous part of the body, and optionally ctx->a[2..3] to reference the
1655 * optional second part if the body wraps at the end of the buffer. An optional
1656 * parameter name is passed in args[0], otherwise any parameter is considered.
1657 */
1658static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1659{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001660 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001661 const char *name;
1662 int name_len;
1663
1664 if (!args || (args[0].type && args[0].type != ARGT_STR))
1665 return 0;
1666
1667 name = "";
1668 name_len = 0;
1669 if (args[0].type == ARGT_STR) {
1670 name = args[0].data.str.area;
1671 name_len = args[0].data.str.data;
1672 }
1673
1674 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001675 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1676 struct buffer *temp;
1677 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001678
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001679 if (!htx)
1680 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001681
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001682 temp = get_trash_chunk();
1683 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1684 struct htx_blk *blk = htx_get_blk(htx, pos);
1685 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001686
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001687 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1688 break;
1689 if (type == HTX_BLK_DATA) {
1690 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
1691 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001692 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001693 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001694
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001695 smp->ctx.a[0] = temp->area;
1696 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001697
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001698 /* Assume that the context is filled with NULL pointer
1699 * before the first call.
1700 * smp->ctx.a[2] = NULL;
1701 * smp->ctx.a[3] = NULL;
1702 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001703
Willy Tarreau79e57332018-10-02 16:01:16 +02001704 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001705
Willy Tarreau79e57332018-10-02 16:01:16 +02001706 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1707}
1708
1709/* Return the signed integer value for the specified url parameter (see url_param
1710 * above).
1711 */
1712static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1713{
1714 int ret = smp_fetch_url_param(args, smp, kw, private);
1715
1716 if (ret > 0) {
1717 smp->data.type = SMP_T_SINT;
1718 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1719 smp->data.u.str.data);
1720 }
1721
1722 return ret;
1723}
1724
1725/* This produces a 32-bit hash of the concatenation of the first occurrence of
1726 * the Host header followed by the path component if it begins with a slash ('/').
1727 * This means that '*' will not be added, resulting in exactly the first Host
1728 * entry. If no Host header is found, then the path is used. The resulting value
1729 * is hashed using the url hash followed by a full avalanche hash and provides a
1730 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1731 * high-traffic sites without having to store whole paths.
1732 * this differs from the base32 functions in that it includes the url parameters
1733 * as well as the path
1734 */
1735static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1736{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001737 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001738 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1739 struct http_hdr_ctx ctx;
1740 struct htx_sl *sl;
1741 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001742 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001743
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001744 if (!htx)
1745 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001746
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001747 ctx.blk = NULL;
1748 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1749 /* OK we have the header value in ctx.value */
1750 while (ctx.value.len--)
1751 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001752 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001753
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001754 /* now retrieve the path */
1755 sl = http_get_stline(htx);
1756 path = http_get_path(htx_sl_req_uri(sl));
1757 while (path.len > 0 && *(path.ptr) != '?') {
1758 path.ptr++;
1759 path.len--;
1760 }
1761 if (path.len && *(path.ptr) == '/') {
1762 while (path.len--)
1763 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001764 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001765
Willy Tarreau79e57332018-10-02 16:01:16 +02001766 hash = full_hash(hash);
1767
1768 smp->data.type = SMP_T_SINT;
1769 smp->data.u.sint = hash;
1770 smp->flags = SMP_F_VOL_1ST;
1771 return 1;
1772}
1773
1774/* This concatenates the source address with the 32-bit hash of the Host and
1775 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1776 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1777 * on the source address length. The URL hash is stored before the address so
1778 * that in environments where IPv6 is insignificant, truncating the output to
1779 * 8 bytes would still work.
1780 */
1781static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1782{
1783 struct buffer *temp;
1784 struct connection *cli_conn = objt_conn(smp->sess->origin);
1785
1786 if (!cli_conn)
1787 return 0;
1788
1789 if (!smp_fetch_url32(args, smp, kw, private))
1790 return 0;
1791
1792 temp = get_trash_chunk();
1793 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1794 temp->data += sizeof(unsigned int);
1795
1796 switch (cli_conn->addr.from.ss_family) {
1797 case AF_INET:
1798 memcpy(temp->area + temp->data,
1799 &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr,
1800 4);
1801 temp->data += 4;
1802 break;
1803 case AF_INET6:
1804 memcpy(temp->area + temp->data,
1805 &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr,
1806 16);
1807 temp->data += 16;
1808 break;
1809 default:
1810 return 0;
1811 }
1812
1813 smp->data.u.str = *temp;
1814 smp->data.type = SMP_T_BIN;
1815 return 1;
1816}
1817
1818/************************************************************************/
1819/* Other utility functions */
1820/************************************************************************/
1821
1822/* This function is used to validate the arguments passed to any "hdr" fetch
1823 * keyword. These keywords support an optional positive or negative occurrence
1824 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
1825 * is assumed that the types are already the correct ones. Returns 0 on error,
1826 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
1827 * error message in case of error, that the caller is responsible for freeing.
1828 * The initial location must either be freeable or NULL.
1829 * Note: this function's pointer is checked from Lua.
1830 */
1831int val_hdr(struct arg *arg, char **err_msg)
1832{
1833 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
1834 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
1835 return 0;
1836 }
1837 return 1;
1838}
1839
1840/************************************************************************/
1841/* All supported sample fetch keywords must be declared here. */
1842/************************************************************************/
1843
1844/* Note: must not be declared <const> as its list will be overwritten */
1845static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
1846 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1847 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
1848 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
1849
1850 /* capture are allocated and are permanent in the stream */
1851 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
1852
1853 /* retrieve these captures from the HTTP logs */
1854 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1855 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1856 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1857
1858 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
1859 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1860
1861 /* cookie is valid in both directions (eg: for "stick ...") but cook*
1862 * are only here to match the ACL's name, are request-only and are used
1863 * for ACL compatibility only.
1864 */
1865 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001866 { "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 +02001867 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1868 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1869
1870 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
1871 * only here to match the ACL's name, are request-only and are used for
1872 * ACL compatibility only.
1873 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001874 { "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 +02001875 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1876 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
1877 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
1878
1879 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
1880 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
1881 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
1882 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
1883 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1884 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1885
1886 /* HTTP protocol on the request path */
1887 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
1888 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
1889
1890 /* HTTP version on the request path */
1891 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1892 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1893
1894 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
1895 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
1896 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
1897 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
1898
1899 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
1900 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
1901
1902 /* HTTP version on the response path */
1903 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
1904 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
1905
1906 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
1907 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
1908 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1909 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1910
1911 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
1912 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1913 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
1914 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1915 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
1916 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
1917 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
1918
1919 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
1920 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
1921 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1922 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1923
1924 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
1925 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1926 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
1927 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1928 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
1929 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
1930 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
1931
1932 /* scook is valid only on the response and is used for ACL compatibility */
1933 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
1934 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1935 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1936 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
1937
1938 /* shdr is valid only on the response and is used for ACL compatibility */
1939 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
1940 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
1941 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
1942 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
1943
1944 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
1945 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
1946 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1947 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
1948 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
1949 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
1950 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
1951 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
1952 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
1953 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1954 { /* END */ },
1955}};
1956
Willy Tarreau0108d902018-11-25 19:14:37 +01001957INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02001958
1959/*
1960 * Local variables:
1961 * c-indent-level: 8
1962 * c-basic-offset: 8
1963 * End:
1964 */