blob: 0989b99cc24ddcc71ec15d7cbc8d39e79a834ecc [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
2 * HTTP samples fetching
3 *
4 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/types.h>
14
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
18
19#include <common/base64.h>
20#include <common/chunk.h>
21#include <common/compat.h>
22#include <common/config.h>
23#include <common/debug.h>
Willy Tarreauafba57a2018-12-11 13:44:24 +010024#include <common/h1.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020025#include <common/http.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010026#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010027#include <common/initcall.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020028#include <common/memory.h>
29#include <common/standard.h>
30#include <common/version.h>
31
32#include <types/global.h>
33
34#include <proto/arg.h>
35#include <proto/auth.h>
Christopher Fauleteb2754b2019-07-16 14:49:01 +020036#include <proto/channel.h>
Willy Tarreau9a1efe12019-07-17 17:13:50 +020037#include <proto/connection.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020038#include <proto/http_fetch.h>
Christopher Fauletef453ed2018-10-24 21:39:27 +020039#include <proto/http_htx.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020040#include <proto/log.h>
41#include <proto/obj_type.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020042#include <proto/http_ana.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020043#include <proto/sample.h>
44#include <proto/stream.h>
45
46
47/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Fauletef453ed2018-10-24 21:39:27 +020048static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
Richard Russo458eafb2019-07-31 11:45:56 -070049/* this is used to convert raw connection buffers to htx */
50static THREAD_LOCAL struct buffer static_raw_htx_chunk;
51static THREAD_LOCAL char *static_raw_htx_buf;
Christopher Fauletef453ed2018-10-24 21:39:27 +020052
Christopher Faulet89dc4992019-04-17 12:02:59 +020053#define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL)
54#define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL)
Willy Tarreau79e57332018-10-02 16:01:16 +020055
Richard Russo458eafb2019-07-31 11:45:56 -070056/* This function returns the static htx chunk, where raw connections get
57 * converted to HTX as needed for samplxsing.
58 */
59struct buffer *get_raw_htx_chunk(void)
60{
61 chunk_reset(&static_raw_htx_chunk);
62 return &static_raw_htx_chunk;
63}
64
65static int alloc_raw_htx_chunk_per_thread()
66{
67 static_raw_htx_buf = malloc(global.tune.bufsize);
68 if (!static_raw_htx_buf)
69 return 0;
70 chunk_init(&static_raw_htx_chunk, static_raw_htx_buf, global.tune.bufsize);
71 return 1;
72}
73
74static void free_raw_htx_chunk_per_thread()
75{
76 free(static_raw_htx_buf);
77 static_raw_htx_buf = NULL;
78}
79
80REGISTER_PER_THREAD_ALLOC(alloc_raw_htx_chunk_per_thread);
81REGISTER_PER_THREAD_FREE(free_raw_htx_chunk_per_thread);
82
Willy Tarreau79e57332018-10-02 16:01:16 +020083/*
84 * Returns the data from Authorization header. Function may be called more
85 * than once so data is stored in txn->auth_data. When no header is found
86 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
87 * searching again for something we are unable to find anyway. However, if
88 * the result if valid, the cache is not reused because we would risk to
89 * have the credentials overwritten by another stream in parallel.
90 */
91
Christopher Fauletcd761952019-07-15 13:58:29 +020092static int get_http_auth(struct sample *smp, struct htx *htx)
Willy Tarreau79e57332018-10-02 16:01:16 +020093{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020094 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020095 struct http_txn *txn = s->txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020096 struct http_hdr_ctx ctx = { .blk = NULL };
97 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020098 struct buffer auth_method;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020099 char *p;
Willy Tarreau79e57332018-10-02 16:01:16 +0200100 int len;
101
102#ifdef DEBUG_AUTH
103 printf("Auth for stream %p: %d\n", s, txn->auth.method);
104#endif
Willy Tarreau79e57332018-10-02 16:01:16 +0200105 if (txn->auth.method == HTTP_AUTH_WRONG)
106 return 0;
107
108 txn->auth.method = HTTP_AUTH_WRONG;
109
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200110 if (txn->flags & TX_USE_PX_CONN)
111 hdr = ist("Proxy-Authorization");
112 else
113 hdr = ist("Authorization");
Willy Tarreau79e57332018-10-02 16:01:16 +0200114
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200115 ctx.blk = NULL;
116 if (!http_find_header(htx, hdr, &ctx, 0))
117 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200118
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200119 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
120 len = p - ctx.value.ptr;
121 if (!p || len <= 0)
122 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200123
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200124 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
125 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200126
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200127 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200128
129 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
130 struct buffer *http_auth = get_trash_chunk();
131
132 len = base64dec(txn->auth.method_data.area,
133 txn->auth.method_data.data,
134 http_auth->area, global.tune.bufsize - 1);
135
136 if (len < 0)
137 return 0;
138
139
140 http_auth->area[len] = '\0';
141
142 p = strchr(http_auth->area, ':');
143
144 if (!p)
145 return 0;
146
147 txn->auth.user = http_auth->area;
148 *p = '\0';
149 txn->auth.pass = p+1;
150
151 txn->auth.method = HTTP_AUTH_BASIC;
152 return 1;
153 }
154
155 return 0;
156}
157
158/* This function ensures that the prerequisites for an L7 fetch are ready,
159 * which means that a request or response is ready. If some data is missing,
160 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200161 * to extract data from L7. If <vol> is non-null during a prefetch, another
162 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200163 *
164 * The function returns :
165 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
166 * decide whether or not an HTTP message is present ;
167 * NULL if the requested data cannot be fetched or if it is certain that
168 * we'll never have any HTTP message there ;
169 * The HTX message if ready
170 */
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200171struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200172{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200173 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200174 struct http_txn *txn = NULL;
175 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200176 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100177 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200178
179 /* Note: it is possible that <s> is NULL when called before stream
180 * initialization (eg: tcp-request connection), so this function is the
181 * one responsible for guarding against this case for all HTTP users.
182 */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200183 if (!s || !chn)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200184 return NULL;
185
186 if (!s->txn) {
187 if (unlikely(!http_alloc_txn(s)))
188 return NULL; /* not enough memory */
189 http_init_txn(s);
190 txn = s->txn;
191 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200192 txn = s->txn;
193 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
194 smp->data.type = SMP_T_BOOL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200195
Christopher Fauleteca88542019-04-03 10:12:42 +0200196 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200197 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200198
Christopher Faulet89dc4992019-04-17 12:02:59 +0200199 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
200 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200201
Christopher Faulet89dc4992019-04-17 12:02:59 +0200202 if (msg->msg_state < HTTP_MSG_BODY) {
203 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200204 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200205 /* Parsing is done by the mux, just wait */
206 smp->flags |= SMP_F_MAY_CHANGE;
207 return NULL;
208 }
209 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200210 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200211 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200212 /* The start-line was already forwarded, it is too late to fetch anything */
213 return NULL;
214 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200215 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200216 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200217 struct buffer *buf;
218 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200219 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200220 union h1_sl h1sl;
221 unsigned int flags = HTX_FL_NONE;
222 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200223
Christopher Faulet89dc4992019-04-17 12:02:59 +0200224 /* no HTTP fetch on the response in TCP mode */
225 if (chn->flags & CF_ISRESP)
226 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200227
Christopher Faulet89dc4992019-04-17 12:02:59 +0200228 /* Now we are working on the request only */
229 buf = &chn->buf;
230 if (b_head(buf) + b_data(buf) > b_wrap(buf))
231 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200232
Christopher Faulet89dc4992019-04-17 12:02:59 +0200233 h1m_init_req(&h1m);
234 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
235 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
236 if (ret <= 0) {
237 /* Invalid or too big*/
238 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200239 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100240
Christopher Faulet89dc4992019-04-17 12:02:59 +0200241 /* wait for a full request */
242 smp->flags |= SMP_F_MAY_CHANGE;
243 return NULL;
244 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100245
Christopher Faulet89dc4992019-04-17 12:02:59 +0200246 /* OK we just got a valid HTTP mesage. We have to convert it
247 * into an HTX message.
248 */
249 if (unlikely(h1sl.rq.v.len == 0)) {
250 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
251 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200252 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200253 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200254 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200255
256 /* Set HTX start-line flags */
257 if (h1m.flags & H1_MF_VER_11)
258 flags |= HTX_SL_F_VER_11;
259 if (h1m.flags & H1_MF_XFER_ENC)
260 flags |= HTX_SL_F_XFER_ENC;
261 flags |= HTX_SL_F_XFER_LEN;
262 if (h1m.flags & H1_MF_CHNK)
263 flags |= HTX_SL_F_CHNK;
264 else if (h1m.flags & H1_MF_CLEN)
265 flags |= HTX_SL_F_CLEN;
266
Richard Russo458eafb2019-07-31 11:45:56 -0700267 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200268 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
269 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200270 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200271 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200272 }
273
274 /* OK we just got a valid HTTP message. If not already done by
275 * HTTP analyzers, we have some minor preparation to perform so
276 * that further checks can rely on HTTP tests.
277 */
278 if (sl && msg->msg_state < HTTP_MSG_BODY) {
279 if (!(chn->flags & CF_ISRESP)) {
280 txn->meth = sl->info.req.meth;
281 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
282 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200283 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200284 else
285 txn->status = sl->info.res.status;
286 if (sl->flags & HTX_SL_F_VER_11)
287 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200288 }
289
290 /* everything's OK */
291 smp->data.u.sint = 1;
292 return htx;
293}
294
Willy Tarreau79e57332018-10-02 16:01:16 +0200295/* This function fetches the method of current HTTP request and stores
296 * it in the global pattern struct as a chunk. There are two possibilities :
297 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
298 * in <len> and <ptr> is NULL ;
299 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
300 * <len> to its length.
301 * This is intended to be used with pat_match_meth() only.
302 */
303static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
304{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200305 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200306 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +0200307 struct http_txn *txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200308 int meth;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200309
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 txn = smp->strm->txn;
314 meth = txn->meth;
315 smp->data.type = SMP_T_METH;
316 smp->data.u.meth.meth = meth;
317 if (meth == HTTP_METH_OTHER) {
318 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200319
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200320 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
321 /* ensure the indexes are not affected */
322 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200323 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200324 sl = http_get_stline(htx);
325 smp->flags |= SMP_F_CONST;
326 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
327 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200328 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200329 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200330 return 1;
331}
332
333static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
334{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200335 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200336 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
337 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200338 char *ptr;
339 int len;
340
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200341 if (!htx)
342 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200343
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200344 sl = http_get_stline(htx);
345 len = HTX_SL_REQ_VLEN(sl);
346 ptr = HTX_SL_REQ_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200347
348 while ((len-- > 0) && (*ptr++ != '/'));
349 if (len <= 0)
350 return 0;
351
352 smp->data.type = SMP_T_STR;
353 smp->data.u.str.area = ptr;
354 smp->data.u.str.data = len;
355
356 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
357 return 1;
358}
359
360static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
361{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200362 struct channel *chn = SMP_RES_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200363 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
364 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200365 char *ptr;
366 int len;
367
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200368 if (!htx)
369 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200370
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200371 sl = http_get_stline(htx);
372 len = HTX_SL_RES_VLEN(sl);
373 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200374
375 while ((len-- > 0) && (*ptr++ != '/'));
376 if (len <= 0)
377 return 0;
378
379 smp->data.type = SMP_T_STR;
380 smp->data.u.str.area = ptr;
381 smp->data.u.str.data = len;
382
383 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
384 return 1;
385}
386
387/* 3. Check on Status Code. We manipulate integers here. */
388static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
389{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200390 struct channel *chn = SMP_RES_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200391 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
392 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200393 char *ptr;
394 int len;
395
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200396 if (!htx)
397 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200398
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200399 sl = http_get_stline(htx);
400 len = HTX_SL_RES_CLEN(sl);
401 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200402
403 smp->data.type = SMP_T_SINT;
404 smp->data.u.sint = __strl2ui(ptr, len);
405 smp->flags = SMP_F_VOL_1ST;
406 return 1;
407}
408
409static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
410{
411 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
412 return 0;
413
414 if (!smp->strm->unique_id) {
415 if ((smp->strm->unique_id = pool_alloc(pool_head_uniqueid)) == NULL)
416 return 0;
417 smp->strm->unique_id[0] = '\0';
418 }
419 smp->data.u.str.data = build_logline(smp->strm, smp->strm->unique_id,
420 UNIQUEID_LEN, &smp->sess->fe->format_unique_id);
421
422 smp->data.type = SMP_T_STR;
423 smp->data.u.str.area = smp->strm->unique_id;
424 smp->flags = SMP_F_CONST;
425 return 1;
426}
427
428/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800429 * empty line which separes headers from the body. This is useful
430 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200431 */
432static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
433{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200434 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200435 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
436 struct buffer *temp;
437 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200438
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200439 if (!htx)
440 return 0;
441 temp = get_trash_chunk();
442 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
443 struct htx_blk *blk = htx_get_blk(htx, pos);
444 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200445
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200446 if (type == HTX_BLK_HDR) {
447 struct ist n = htx_get_blk_name(htx, blk);
448 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200449
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200450 if (!htx_hdr_to_h1(n, v, temp))
451 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200452 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200453 else if (type == HTX_BLK_EOH) {
454 if (!chunk_memcat(temp, "\r\n", 2))
455 return 0;
456 break;
457 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200458 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200459 smp->data.type = SMP_T_STR;
460 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200461 return 1;
462}
463
464/* Returns the header request in a length/value encoded format.
465 * This is useful for exchanges with the SPOE.
466 *
467 * A "length value" is a multibyte code encoding numbers. It uses the
468 * SPOE format. The encoding is the following:
469 *
470 * Each couple "header name" / "header value" is composed
471 * like this:
472 * "length value" "header name bytes"
473 * "length value" "header value bytes"
474 * When the last header is reached, the header name and the header
475 * value are empty. Their length are 0
476 */
477static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
478{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200479 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200480 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200481 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200482 char *p, *end;
483 int32_t pos;
484 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200485
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200486 if (!htx)
487 return 0;
488 temp = get_trash_chunk();
489 p = temp->area;
490 end = temp->area + temp->size;
491 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
492 struct htx_blk *blk = htx_get_blk(htx, pos);
493 enum htx_blk_type type = htx_get_blk_type(blk);
494 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200495
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200496 if (type == HTX_BLK_HDR) {
497 n = htx_get_blk_name(htx,blk);
498 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200499
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200500 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200501 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200502 if (ret == -1)
503 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200504 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200505 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200506 memcpy(p, n.ptr, n.len);
507 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200508
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200509 /* encode the header value. */
510 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200511 if (ret == -1)
512 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200513 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200514 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200515 memcpy(p, v.ptr, v.len);
516 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200517
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200518 }
519 else if (type == HTX_BLK_EOH) {
520 /* encode the end of the header list with empty
521 * header name and header value.
522 */
523 ret = encode_varint(0, &p, end);
524 if (ret == -1)
525 return 0;
526 ret = encode_varint(0, &p, end);
527 if (ret == -1)
528 return 0;
529 break;
530 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200531 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200532
533 /* Initialise sample data which will be filled. */
534 smp->data.type = SMP_T_BIN;
535 smp->data.u.str.area = temp->area;
536 smp->data.u.str.data = p - temp->area;
537 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200538 return 1;
539}
540
541/* returns the longest available part of the body. This requires that the body
542 * has been waited for using http-buffer-request.
543 */
544static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
545{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200546 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200547 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200548 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200549 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200550
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200551 if (!htx)
552 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200553
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200554 temp = get_trash_chunk();
555 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);
Willy Tarreau79e57332018-10-02 16:01:16 +0200558
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 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
563 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200564 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200565 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200566
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200567 smp->data.type = SMP_T_BIN;
568 smp->data.u.str = *temp;
569 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200570 return 1;
571}
572
573
574/* returns the available length of the body. This requires that the body
575 * has been waited for using http-buffer-request.
576 */
577static int smp_fetch_body_len(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 Fauletc16317d2018-12-12 14:11:22 +0100583
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 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200596
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200597 smp->data.type = SMP_T_SINT;
598 smp->data.u.sint = len;
599 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200600 return 1;
601}
602
603
604/* returns the advertised length of the body, or the advertised size of the
605 * chunks available in the buffer. This requires that the body has been waited
606 * for using http-buffer-request.
607 */
608static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
609{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200610 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200611 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
612 int32_t pos;
613 unsigned long long len = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200614
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200615 if (!htx)
616 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100617
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200618 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
619 struct htx_blk *blk = htx_get_blk(htx, pos);
620 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100621
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200622 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
623 break;
624 if (type == HTX_BLK_DATA)
625 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200626 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200627 if (htx->extra != ULLONG_MAX)
628 len += htx->extra;
Willy Tarreau79e57332018-10-02 16:01:16 +0200629
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200630 smp->data.type = SMP_T_SINT;
631 smp->data.u.sint = len;
632 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200633 return 1;
634}
635
636
637/* 4. Check on URL/URI. A pointer to the URI is stored. */
638static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
639{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200640 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200641 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
642 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200643
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200644 if (!htx)
645 return 0;
646 sl = http_get_stline(htx);
647 smp->data.type = SMP_T_STR;
648 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
649 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
650 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200651 return 1;
652}
653
654static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
655{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200656 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200657 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
658 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200659 struct sockaddr_storage addr;
660
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200661 if (!htx)
662 return 0;
663 sl = http_get_stline(htx);
664 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +0200665
Willy Tarreau79e57332018-10-02 16:01:16 +0200666 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
667 return 0;
668
669 smp->data.type = SMP_T_IPV4;
670 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
671 smp->flags = 0;
672 return 1;
673}
674
675static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
676{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200677 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200678 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
679 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200680 struct sockaddr_storage addr;
681
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200682 if (!htx)
683 return 0;
684 sl = http_get_stline(htx);
685 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200686
Willy Tarreau79e57332018-10-02 16:01:16 +0200687 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
688 return 0;
689
690 smp->data.type = SMP_T_SINT;
691 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
692 smp->flags = 0;
693 return 1;
694}
695
696/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
697 * Accepts an optional argument of type string containing the header field name,
698 * and an optional argument of type signed or unsigned integer to request an
699 * explicit occurrence of the header. Note that in the event of a missing name,
700 * headers are considered from the first one. It does not stop on commas and
701 * returns full lines instead (useful for User-Agent or Date for example).
702 */
703static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
704{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200705 /* possible keywords: req.fhdr, res.fhdr */
706 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200707 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
708 struct http_hdr_ctx *ctx = smp->ctx.a[0];
709 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200710 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200711
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200712 if (!ctx) {
713 /* first call */
714 ctx = &static_http_hdr_ctx;
715 ctx->blk = NULL;
716 smp->ctx.a[0] = ctx;
717 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200718
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200719 if (args) {
720 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200721 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200722 name.ptr = args[0].data.str.area;
723 name.len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +0200724
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200725 if (args[1].type == ARGT_SINT)
726 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200727 }
728
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200729 if (!htx)
730 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200731
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200732 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
733 /* search for header from the beginning */
734 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200735
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200736 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
737 /* no explicit occurrence and single fetch => last header by default */
738 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200739
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200740 if (!occ)
741 /* prepare to report multiple occurrences for ACL fetches */
742 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200743
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200744 smp->data.type = SMP_T_STR;
745 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
746 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
747 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200748 smp->flags &= ~SMP_F_NOT_LAST;
749 return 0;
750}
751
752/* 6. Check on HTTP header count. The number of occurrences is returned.
753 * Accepts exactly 1 argument of type string. It does not stop on commas and
754 * returns full lines instead (useful for User-Agent or Date for example).
755 */
756static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
757{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200758 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
759 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200760 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
761 struct http_hdr_ctx ctx;
762 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200763 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200764
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200765 if (!htx)
766 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200767
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200768 if (args && args->type == ARGT_STR) {
769 name.ptr = args->data.str.area;
770 name.len = args->data.str.data;
771 } else {
772 name.ptr = NULL;
773 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200774 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200775
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200776 ctx.blk = NULL;
777 cnt = 0;
778 while (http_find_header(htx, name, &ctx, 1))
779 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200780 smp->data.type = SMP_T_SINT;
781 smp->data.u.sint = cnt;
782 smp->flags = SMP_F_VOL_HDR;
783 return 1;
784}
785
786static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
787{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200788 /* possible keywords: req.hdr_names, res.hdr_names */
789 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200790 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200791 struct buffer *temp;
792 char del = ',';
793
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200794 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200795
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200796 if (!htx)
797 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200798
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200799 if (args && args->type == ARGT_STR)
800 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200801
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200802 temp = get_trash_chunk();
803 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
804 struct htx_blk *blk = htx_get_blk(htx, pos);
805 enum htx_blk_type type = htx_get_blk_type(blk);
806 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200807
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200808 if (type == HTX_BLK_EOH)
809 break;
810 if (type != HTX_BLK_HDR)
811 continue;
812 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200813
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200814 if (temp->data)
815 temp->area[temp->data++] = del;
816 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200817 }
818
819 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200820 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200821 smp->flags = SMP_F_VOL_HDR;
822 return 1;
823}
824
825/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
826 * Accepts an optional argument of type string containing the header field name,
827 * and an optional argument of type signed or unsigned integer to request an
828 * explicit occurrence of the header. Note that in the event of a missing name,
829 * headers are considered from the first one.
830 */
831static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
832{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200833 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
834 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200835 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
836 struct http_hdr_ctx *ctx = smp->ctx.a[0];
837 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200838 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200839
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200840 if (!ctx) {
841 /* first call */
842 ctx = &static_http_hdr_ctx;
843 ctx->blk = NULL;
844 smp->ctx.a[0] = ctx;
845 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200846
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200847 if (args) {
848 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200849 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200850 name.ptr = args[0].data.str.area;
851 name.len = args[0].data.str.data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200852
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200853 if (args[1].type == ARGT_SINT)
854 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200855 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200856
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200857 if (!htx)
858 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200859
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200860 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
861 /* search for header from the beginning */
862 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200863
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200864 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
865 /* no explicit occurrence and single fetch => last header by default */
866 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200867
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200868 if (!occ)
869 /* prepare to report multiple occurrences for ACL fetches */
870 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200871
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200872 smp->data.type = SMP_T_STR;
873 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
874 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
875 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200876
877 smp->flags &= ~SMP_F_NOT_LAST;
878 return 0;
879}
880
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200881/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
882 * the right channel. So instead of duplicating the code, we just change the
883 * keyword and then fallback on smp_fetch_hdr().
884 */
885static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
886{
887 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
888 return smp_fetch_hdr(args, smp, kw, private);
889}
890
Willy Tarreau79e57332018-10-02 16:01:16 +0200891/* 6. Check on HTTP header count. The number of occurrences is returned.
892 * Accepts exactly 1 argument of type string.
893 */
894static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
895{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200896 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
897 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200898 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
899 struct http_hdr_ctx ctx;
900 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200901 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200902
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200903 if (!htx)
904 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200905
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200906 if (args && args->type == ARGT_STR) {
907 name.ptr = args->data.str.area;
908 name.len = args->data.str.data;
909 } else {
910 name.ptr = NULL;
911 name.len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200912 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200913
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200914 ctx.blk = NULL;
915 cnt = 0;
916 while (http_find_header(htx, name, &ctx, 0))
917 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200918
919 smp->data.type = SMP_T_SINT;
920 smp->data.u.sint = cnt;
921 smp->flags = SMP_F_VOL_HDR;
922 return 1;
923}
924
925/* Fetch an HTTP header's integer value. The integer value is returned. It
926 * takes a mandatory argument of type string and an optional one of type int
927 * to designate a specific occurrence. It returns an unsigned integer, which
928 * may or may not be appropriate for everything.
929 */
930static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
931{
932 int ret = smp_fetch_hdr(args, smp, kw, private);
933
934 if (ret > 0) {
935 smp->data.type = SMP_T_SINT;
936 smp->data.u.sint = strl2ic(smp->data.u.str.area,
937 smp->data.u.str.data);
938 }
939
940 return ret;
941}
942
943/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
944 * and an optional one of type int to designate a specific occurrence.
945 * It returns an IPv4 or IPv6 address.
946 */
947static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
948{
949 int ret;
950
951 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
952 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
953 smp->data.type = SMP_T_IPV4;
954 break;
955 } else {
956 struct buffer *temp = get_trash_chunk();
957 if (smp->data.u.str.data < temp->size - 1) {
958 memcpy(temp->area, smp->data.u.str.area,
959 smp->data.u.str.data);
960 temp->area[smp->data.u.str.data] = '\0';
961 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
962 smp->data.type = SMP_T_IPV6;
963 break;
964 }
965 }
966 }
967
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200968 /* if the header doesn't match an IP address, fetch next one */
969 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200970 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200971 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200972 return ret;
973}
Willy Tarreau79e57332018-10-02 16:01:16 +0200974
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200975/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
976 * the first '/' after the possible hostname, and ends before the possible '?'.
977 */
978static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
979{
980 struct channel *chn = SMP_REQ_CHN(smp);
981 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
982 struct htx_sl *sl;
983 struct ist path;
984 size_t len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200985
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200986 if (!htx)
987 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200988
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200989 sl = http_get_stline(htx);
990 path = http_get_path(htx_sl_req_uri(sl));
991 if (!path.ptr)
992 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200993
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200994 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
995 ;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200996
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200997 /* OK, we got the '/' ! */
998 smp->data.type = SMP_T_STR;
999 smp->data.u.str.area = path.ptr;
1000 smp->data.u.str.data = len;
1001 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001002 return 1;
1003}
1004
1005/* This produces a concatenation of the first occurrence of the Host header
1006 * followed by the path component if it begins with a slash ('/'). This means
1007 * that '*' will not be added, resulting in exactly the first Host entry.
1008 * If no Host header is found, then the path is returned as-is. The returned
1009 * value is stored in the trash so it does not need to be marked constant.
1010 * The returned sample is of type string.
1011 */
1012static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1013{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001014 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001015 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1016 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001017 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001018 struct http_hdr_ctx ctx;
1019 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001020
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001021 if (!htx)
1022 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001023
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001024 ctx.blk = NULL;
1025 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1026 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001027
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001028 /* OK we have the header value in ctx.value */
1029 temp = get_trash_chunk();
1030 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001031
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001032 /* now retrieve the path */
1033 sl = http_get_stline(htx);
1034 path = http_get_path(htx_sl_req_uri(sl));
1035 if (path.ptr) {
1036 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001037
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001038 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1039 ;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001040
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001041 if (len && *(path.ptr) == '/')
1042 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001043 }
1044
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001045 smp->data.type = SMP_T_STR;
1046 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001047 smp->flags = SMP_F_VOL_1ST;
1048 return 1;
1049}
1050
1051/* This produces a 32-bit hash of the concatenation of the first occurrence of
1052 * the Host header followed by the path component if it begins with a slash ('/').
1053 * This means that '*' will not be added, resulting in exactly the first Host
1054 * entry. If no Host header is found, then the path is used. The resulting value
1055 * is hashed using the path hash followed by a full avalanche hash and provides a
1056 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1057 * high-traffic sites without having to store whole paths.
1058 */
1059static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1060{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001061 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001062 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1063 struct htx_sl *sl;
1064 struct http_hdr_ctx ctx;
1065 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001066 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001067
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001068 if (!htx)
1069 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001070
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001071 ctx.blk = NULL;
1072 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1073 /* OK we have the header value in ctx.value */
1074 while (ctx.value.len--)
1075 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001076 }
1077
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001078 /* now retrieve the path */
1079 sl = http_get_stline(htx);
1080 path = http_get_path(htx_sl_req_uri(sl));
1081 if (path.ptr) {
1082 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001083
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001084 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1085 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001086
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001087 if (len && *(path.ptr) == '/') {
1088 while (len--)
1089 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001090 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001091 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001092
Willy Tarreau79e57332018-10-02 16:01:16 +02001093 hash = full_hash(hash);
1094
1095 smp->data.type = SMP_T_SINT;
1096 smp->data.u.sint = hash;
1097 smp->flags = SMP_F_VOL_1ST;
1098 return 1;
1099}
1100
1101/* This concatenates the source address with the 32-bit hash of the Host and
1102 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1103 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1104 * on the source address length. The path hash is stored before the address so
1105 * that in environments where IPv6 is insignificant, truncating the output to
1106 * 8 bytes would still work.
1107 */
1108static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1109{
1110 struct buffer *temp;
1111 struct connection *cli_conn = objt_conn(smp->sess->origin);
1112
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001113 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001114 return 0;
1115
1116 if (!smp_fetch_base32(args, smp, kw, private))
1117 return 0;
1118
1119 temp = get_trash_chunk();
1120 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1121 temp->data += sizeof(unsigned int);
1122
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001123 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001124 case AF_INET:
1125 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001126 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001127 4);
1128 temp->data += 4;
1129 break;
1130 case AF_INET6:
1131 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001132 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001133 16);
1134 temp->data += 16;
1135 break;
1136 default:
1137 return 0;
1138 }
1139
1140 smp->data.u.str = *temp;
1141 smp->data.type = SMP_T_BIN;
1142 return 1;
1143}
1144
1145/* Extracts the query string, which comes after the question mark '?'. If no
1146 * question mark is found, nothing is returned. Otherwise it returns a sample
1147 * of type string carrying the whole query string.
1148 */
1149static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1150{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001151 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001152 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1153 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001154 char *ptr, *end;
1155
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001156 if (!htx)
1157 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001158
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001159 sl = http_get_stline(htx);
1160 ptr = HTX_SL_REQ_UPTR(sl);
1161 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001162
1163 /* look up the '?' */
1164 do {
1165 if (ptr == end)
1166 return 0;
1167 } while (*ptr++ != '?');
1168
1169 smp->data.type = SMP_T_STR;
1170 smp->data.u.str.area = ptr;
1171 smp->data.u.str.data = end - ptr;
1172 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1173 return 1;
1174}
1175
1176static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1177{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001178 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001179 struct htx *htx = smp_prefetch_htx(smp, chn, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001180
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001181 if (!htx)
1182 return 0;
1183 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001184 smp->data.u.sint = 1;
1185 return 1;
1186}
1187
1188/* return a valid test if the current request is the first one on the connection */
1189static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1190{
1191 smp->data.type = SMP_T_BOOL;
1192 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1193 return 1;
1194}
1195
Christopher Fauleta4063562019-08-02 11:51:37 +02001196/* Fetch the authentication method if there is an Authorization header. It
1197 * relies on get_http_auth()
1198 */
1199static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1200{
1201 struct channel *chn = SMP_REQ_CHN(smp);
1202 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1203 struct http_txn *txn;
1204
1205 if (!htx)
1206 return 0;
1207
1208 txn = smp->strm->txn;
1209 if (!get_http_auth(smp, htx))
1210 return 0;
1211
1212 switch (txn->auth.method) {
1213 case HTTP_AUTH_BASIC:
1214 smp->data.u.str.area = "Basic";
1215 smp->data.u.str.data = 5;
1216 break;
1217 case HTTP_AUTH_DIGEST:
1218 /* Unexpected because not supported */
1219 smp->data.u.str.area = "Digest";
1220 smp->data.u.str.data = 6;
1221 break;
1222 default:
1223 return 0;
1224 }
1225
1226 smp->data.type = SMP_T_STR;
1227 smp->flags = SMP_F_CONST;
1228 return 1;
1229}
1230
1231/* Fetch the user supplied if there is an Authorization header. It relies on
1232 * get_http_auth()
1233 */
1234static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1235{
1236 struct channel *chn = SMP_REQ_CHN(smp);
1237 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1238 struct http_txn *txn;
1239
1240 if (!htx)
1241 return 0;
1242
1243 txn = smp->strm->txn;
1244 if (!get_http_auth(smp, htx))
1245 return 0;
1246
1247 smp->data.type = SMP_T_STR;
1248 smp->data.u.str.area = txn->auth.user;
1249 smp->data.u.str.data = strlen(txn->auth.user);
1250 smp->flags = SMP_F_CONST;
1251 return 1;
1252}
1253
1254/* Fetch the password supplied if there is an Authorization header. It relies on
1255 * get_http_auth()
1256 */
1257static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1258{
1259 struct channel *chn = SMP_REQ_CHN(smp);
1260 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1261 struct http_txn *txn;
1262
1263 if (!htx)
1264 return 0;
1265
1266 txn = smp->strm->txn;
1267 if (!get_http_auth(smp, htx))
1268 return 0;
1269
1270 smp->data.type = SMP_T_STR;
1271 smp->data.u.str.area = txn->auth.pass;
1272 smp->data.u.str.data = strlen(txn->auth.pass);
1273 smp->flags = SMP_F_CONST;
1274 return 1;
1275}
1276
Willy Tarreau79e57332018-10-02 16:01:16 +02001277/* Accepts exactly 1 argument of type userlist */
1278static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1279{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001280 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001281 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001282
1283 if (!args || args->type != ARGT_USR)
1284 return 0;
1285
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001286 if (!htx)
1287 return 0;
1288 if (!get_http_auth(smp, htx))
1289 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001290
1291 smp->data.type = SMP_T_BOOL;
1292 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001293 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001294 return 1;
1295}
1296
1297/* Accepts exactly 1 argument of type userlist */
1298static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1299{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001300 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001301 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001302
Willy Tarreau79e57332018-10-02 16:01:16 +02001303 if (!args || args->type != ARGT_USR)
1304 return 0;
1305
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001306 if (!htx)
1307 return 0;
1308 if (!get_http_auth(smp, htx))
1309 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001310
Willy Tarreau79e57332018-10-02 16:01:16 +02001311 /* if the user does not belong to the userlist or has a wrong password,
1312 * report that it unconditionally does not match. Otherwise we return
1313 * a string containing the username.
1314 */
1315 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1316 smp->strm->txn->auth.pass))
1317 return 0;
1318
1319 /* pat_match_auth() will need the user list */
1320 smp->ctx.a[0] = args->data.usr;
1321
1322 smp->data.type = SMP_T_STR;
1323 smp->flags = SMP_F_CONST;
1324 smp->data.u.str.area = smp->strm->txn->auth.user;
1325 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1326
1327 return 1;
1328}
1329
1330/* Fetch a captured HTTP request header. The index is the position of
1331 * the "capture" option in the configuration file
1332 */
1333static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1334{
1335 struct proxy *fe = strm_fe(smp->strm);
1336 int idx;
1337
1338 if (!args || args->type != ARGT_SINT)
1339 return 0;
1340
1341 idx = args->data.sint;
1342
1343 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1344 return 0;
1345
1346 smp->data.type = SMP_T_STR;
1347 smp->flags |= SMP_F_CONST;
1348 smp->data.u.str.area = smp->strm->req_cap[idx];
1349 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1350
1351 return 1;
1352}
1353
1354/* Fetch a captured HTTP response header. The index is the position of
1355 * the "capture" option in the configuration file
1356 */
1357static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1358{
1359 struct proxy *fe = strm_fe(smp->strm);
1360 int idx;
1361
1362 if (!args || args->type != ARGT_SINT)
1363 return 0;
1364
1365 idx = args->data.sint;
1366
1367 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1368 return 0;
1369
1370 smp->data.type = SMP_T_STR;
1371 smp->flags |= SMP_F_CONST;
1372 smp->data.u.str.area = smp->strm->res_cap[idx];
1373 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1374
1375 return 1;
1376}
1377
1378/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1379static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1380{
1381 struct buffer *temp;
1382 struct http_txn *txn = smp->strm->txn;
1383 char *ptr;
1384
1385 if (!txn || !txn->uri)
1386 return 0;
1387
1388 ptr = txn->uri;
1389
1390 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1391 ptr++;
1392
1393 temp = get_trash_chunk();
1394 temp->area = txn->uri;
1395 temp->data = ptr - txn->uri;
1396 smp->data.u.str = *temp;
1397 smp->data.type = SMP_T_STR;
1398 smp->flags = SMP_F_CONST;
1399
1400 return 1;
1401
1402}
1403
1404/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1405static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1406{
1407 struct http_txn *txn = smp->strm->txn;
1408 struct ist path;
1409 const char *ptr;
1410
1411 if (!txn || !txn->uri)
1412 return 0;
1413
1414 ptr = txn->uri;
1415
1416 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1417 ptr++;
1418
1419 if (!*ptr)
1420 return 0;
1421
Christopher Faulet78337bb2018-11-15 14:35:18 +01001422 /* skip the first space and find space after URI */
1423 path = ist2(++ptr, 0);
1424 while (*ptr != ' ' && *ptr != '\0')
1425 ptr++;
1426 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001427
Christopher Faulet78337bb2018-11-15 14:35:18 +01001428 path = http_get_path(path);
Willy Tarreau79e57332018-10-02 16:01:16 +02001429 if (!path.ptr)
1430 return 0;
1431
1432 smp->data.u.str.area = path.ptr;
1433 smp->data.u.str.data = path.len;
1434 smp->data.type = SMP_T_STR;
1435 smp->flags = SMP_F_CONST;
1436
1437 return 1;
1438}
1439
1440/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1441 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1442 */
1443static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1444{
1445 struct http_txn *txn = smp->strm->txn;
1446
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001447 if (!txn || txn->req.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001448 return 0;
1449
1450 if (txn->req.flags & HTTP_MSGF_VER_11)
1451 smp->data.u.str.area = "HTTP/1.1";
1452 else
1453 smp->data.u.str.area = "HTTP/1.0";
1454
1455 smp->data.u.str.data = 8;
1456 smp->data.type = SMP_T_STR;
1457 smp->flags = SMP_F_CONST;
1458 return 1;
1459
1460}
1461
1462/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1463 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1464 */
1465static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1466{
1467 struct http_txn *txn = smp->strm->txn;
1468
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001469 if (!txn || txn->rsp.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001470 return 0;
1471
1472 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1473 smp->data.u.str.area = "HTTP/1.1";
1474 else
1475 smp->data.u.str.area = "HTTP/1.0";
1476
1477 smp->data.u.str.data = 8;
1478 smp->data.type = SMP_T_STR;
1479 smp->flags = SMP_F_CONST;
1480 return 1;
1481
1482}
1483
1484/* Iterate over all cookies present in a message. The context is stored in
1485 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1486 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1487 * the direction, multiple cookies may be parsed on the same line or not.
1488 * The cookie name is in args and the name length in args->data.str.len.
1489 * Accepts exactly 1 argument of type string. If the input options indicate
1490 * that no iterating is desired, then only last value is fetched if any.
1491 * The returned sample is of type CSTR. Can be used to parse cookies in other
1492 * files.
1493 */
1494static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1495{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001496 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1497 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001498 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1499 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1500 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001501 int occ = 0;
1502 int found = 0;
1503
1504 if (!args || args->type != ARGT_STR)
1505 return 0;
1506
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001507 if (!ctx) {
1508 /* first call */
1509 ctx = &static_http_hdr_ctx;
1510 ctx->blk = NULL;
1511 smp->ctx.a[2] = ctx;
1512 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001513
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001514 if (!htx)
1515 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001516
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001517 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001518
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001519 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1520 /* no explicit occurrence and single fetch => last cookie by default */
1521 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001522
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001523 /* OK so basically here, either we want only one value and it's the
1524 * last one, or we want to iterate over all of them and we fetch the
1525 * next one.
1526 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001527
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001528 if (!(smp->flags & SMP_F_NOT_LAST)) {
1529 /* search for the header from the beginning, we must first initialize
1530 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001531 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001532 smp->ctx.a[0] = NULL;
1533 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001534 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001535
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001536 smp->flags |= SMP_F_VOL_HDR;
1537 while (1) {
1538 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1539 if (!smp->ctx.a[0]) {
1540 if (!http_find_header(htx, hdr, ctx, 0))
1541 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001542
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001543 if (ctx->value.len < args->data.str.data + 1)
1544 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001545
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001546 smp->ctx.a[0] = ctx->value.ptr;
1547 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001548 }
1549
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001550 smp->data.type = SMP_T_STR;
1551 smp->flags |= SMP_F_CONST;
1552 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
1553 args->data.str.area, args->data.str.data,
1554 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1555 &smp->data.u.str.area,
1556 &smp->data.u.str.data);
1557 if (smp->ctx.a[0]) {
1558 found = 1;
1559 if (occ >= 0) {
1560 /* one value was returned into smp->data.u.str.{str,len} */
1561 smp->flags |= SMP_F_NOT_LAST;
1562 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001563 }
1564 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001565 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001566 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001567
Willy Tarreau79e57332018-10-02 16:01:16 +02001568 /* all cookie headers and values were scanned. If we're looking for the
1569 * last occurrence, we may return it now.
1570 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001571 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001572 smp->flags &= ~SMP_F_NOT_LAST;
1573 return found;
1574}
1575
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001576/* Same than smp_fetch_cookie() but only relies on the sample direction to
1577 * choose the right channel. So instead of duplicating the code, we just change
1578 * the keyword and then fallback on smp_fetch_cookie().
1579 */
1580static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1581{
1582 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1583 return smp_fetch_cookie(args, smp, kw, private);
1584}
1585
Willy Tarreau79e57332018-10-02 16:01:16 +02001586/* Iterate over all cookies present in a request to count how many occurrences
1587 * match the name in args and args->data.str.len. If <multi> is non-null, then
1588 * multiple cookies may be parsed on the same line. The returned sample is of
1589 * type UINT. Accepts exactly 1 argument of type string.
1590 */
1591static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1592{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001593 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1594 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001595 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1596 struct http_hdr_ctx ctx;
1597 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001598 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001599 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001600
1601 if (!args || args->type != ARGT_STR)
1602 return 0;
1603
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001604 if (!htx)
1605 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001606
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001607 hdr = (!(chn->flags & CF_ISRESP) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001608
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001609 val_end = val_beg = NULL;
1610 ctx.blk = NULL;
1611 cnt = 0;
1612 while (1) {
1613 /* Note: val_beg == NULL every time we need to fetch a new header */
1614 if (!val_beg) {
1615 if (!http_find_header(htx, hdr, &ctx, 0))
1616 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001617
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001618 if (ctx.value.len < args->data.str.data + 1)
1619 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001620
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001621 val_beg = ctx.value.ptr;
1622 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001623 }
1624
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001625 smp->data.type = SMP_T_STR;
1626 smp->flags |= SMP_F_CONST;
1627 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
1628 args->data.str.area, args->data.str.data,
1629 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1630 &smp->data.u.str.area,
1631 &smp->data.u.str.data))) {
1632 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001633 }
1634 }
1635
1636 smp->data.type = SMP_T_SINT;
1637 smp->data.u.sint = cnt;
1638 smp->flags |= SMP_F_VOL_HDR;
1639 return 1;
1640}
1641
1642/* Fetch an cookie's integer value. The integer value is returned. It
1643 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1644 */
1645static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1646{
1647 int ret = smp_fetch_cookie(args, smp, kw, private);
1648
1649 if (ret > 0) {
1650 smp->data.type = SMP_T_SINT;
1651 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1652 smp->data.u.str.data);
1653 }
1654
1655 return ret;
1656}
1657
1658/************************************************************************/
1659/* The code below is dedicated to sample fetches */
1660/************************************************************************/
1661
1662/* This scans a URL-encoded query string. It takes an optionally wrapping
1663 * string whose first contigous chunk has its beginning in ctx->a[0] and end
1664 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1665 * pointers are updated for next iteration before leaving.
1666 */
1667static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1668{
1669 const char *vstart, *vend;
1670 struct buffer *temp;
1671 const char **chunks = (const char **)smp->ctx.a;
1672
1673 if (!http_find_next_url_param(chunks, name, name_len,
1674 &vstart, &vend, delim))
1675 return 0;
1676
1677 /* Create sample. If the value is contiguous, return the pointer as CONST,
1678 * if the value is wrapped, copy-it in a buffer.
1679 */
1680 smp->data.type = SMP_T_STR;
1681 if (chunks[2] &&
1682 vstart >= chunks[0] && vstart <= chunks[1] &&
1683 vend >= chunks[2] && vend <= chunks[3]) {
1684 /* Wrapped case. */
1685 temp = get_trash_chunk();
1686 memcpy(temp->area, vstart, chunks[1] - vstart);
1687 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1688 vend - chunks[2]);
1689 smp->data.u.str.area = temp->area;
1690 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1691 } else {
1692 /* Contiguous case. */
1693 smp->data.u.str.area = (char *)vstart;
1694 smp->data.u.str.data = vend - vstart;
1695 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1696 }
1697
1698 /* Update context, check wrapping. */
1699 chunks[0] = vend;
1700 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1701 chunks[1] = chunks[3];
1702 chunks[2] = NULL;
1703 }
1704
1705 if (chunks[0] < chunks[1])
1706 smp->flags |= SMP_F_NOT_LAST;
1707
1708 return 1;
1709}
1710
1711/* This function iterates over each parameter of the query string. It uses
1712 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1713 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1714 * An optional parameter name is passed in args[0], otherwise any parameter is
1715 * considered. It supports an optional delimiter argument for the beginning of
1716 * the string in args[1], which defaults to "?".
1717 */
1718static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1719{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001720 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001721 char delim = '?';
1722 const char *name;
1723 int name_len;
1724
1725 if (!args ||
1726 (args[0].type && args[0].type != ARGT_STR) ||
1727 (args[1].type && args[1].type != ARGT_STR))
1728 return 0;
1729
1730 name = "";
1731 name_len = 0;
1732 if (args->type == ARGT_STR) {
1733 name = args->data.str.area;
1734 name_len = args->data.str.data;
1735 }
1736
1737 if (args[1].type)
1738 delim = *args[1].data.str.area;
1739
1740 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001741 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1742 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +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 sl = http_get_stline(htx);
1748 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1749 if (!smp->ctx.a[0])
1750 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001751
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001752 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001753
1754 /* Assume that the context is filled with NULL pointer
1755 * before the first call.
1756 * smp->ctx.a[2] = NULL;
1757 * smp->ctx.a[3] = NULL;
1758 */
1759 }
1760
1761 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1762}
1763
1764/* This function iterates over each parameter of the body. This requires
1765 * that the body has been waited for using http-buffer-request. It uses
1766 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
1767 * contigous part of the body, and optionally ctx->a[2..3] to reference the
1768 * optional second part if the body wraps at the end of the buffer. An optional
1769 * parameter name is passed in args[0], otherwise any parameter is considered.
1770 */
1771static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1772{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001773 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001774 const char *name;
1775 int name_len;
1776
1777 if (!args || (args[0].type && args[0].type != ARGT_STR))
1778 return 0;
1779
1780 name = "";
1781 name_len = 0;
1782 if (args[0].type == ARGT_STR) {
1783 name = args[0].data.str.area;
1784 name_len = args[0].data.str.data;
1785 }
1786
1787 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001788 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1789 struct buffer *temp;
1790 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001791
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001792 if (!htx)
1793 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001794
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001795 temp = get_trash_chunk();
1796 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1797 struct htx_blk *blk = htx_get_blk(htx, pos);
1798 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001799
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001800 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1801 break;
1802 if (type == HTX_BLK_DATA) {
1803 if (!htx_data_to_h1(htx_get_blk_value(htx, blk), temp, 0))
1804 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001805 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001806 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001807
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001808 smp->ctx.a[0] = temp->area;
1809 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001810
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001811 /* Assume that the context is filled with NULL pointer
1812 * before the first call.
1813 * smp->ctx.a[2] = NULL;
1814 * smp->ctx.a[3] = NULL;
1815 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001816
Willy Tarreau79e57332018-10-02 16:01:16 +02001817 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001818
Willy Tarreau79e57332018-10-02 16:01:16 +02001819 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1820}
1821
1822/* Return the signed integer value for the specified url parameter (see url_param
1823 * above).
1824 */
1825static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1826{
1827 int ret = smp_fetch_url_param(args, smp, kw, private);
1828
1829 if (ret > 0) {
1830 smp->data.type = SMP_T_SINT;
1831 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1832 smp->data.u.str.data);
1833 }
1834
1835 return ret;
1836}
1837
1838/* This produces a 32-bit hash of the concatenation of the first occurrence of
1839 * the Host header followed by the path component if it begins with a slash ('/').
1840 * This means that '*' will not be added, resulting in exactly the first Host
1841 * entry. If no Host header is found, then the path is used. The resulting value
1842 * is hashed using the url hash followed by a full avalanche hash and provides a
1843 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1844 * high-traffic sites without having to store whole paths.
1845 * this differs from the base32 functions in that it includes the url parameters
1846 * as well as the path
1847 */
1848static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1849{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001850 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001851 struct htx *htx = smp_prefetch_htx(smp, chn, 1);
1852 struct http_hdr_ctx ctx;
1853 struct htx_sl *sl;
1854 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001855 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001856
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001857 if (!htx)
1858 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001859
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001860 ctx.blk = NULL;
1861 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1862 /* OK we have the header value in ctx.value */
1863 while (ctx.value.len--)
1864 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001865 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001866
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001867 /* now retrieve the path */
1868 sl = http_get_stline(htx);
1869 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001870 if (path.len && *(path.ptr) == '/') {
1871 while (path.len--)
1872 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001873 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001874
Willy Tarreau79e57332018-10-02 16:01:16 +02001875 hash = full_hash(hash);
1876
1877 smp->data.type = SMP_T_SINT;
1878 smp->data.u.sint = hash;
1879 smp->flags = SMP_F_VOL_1ST;
1880 return 1;
1881}
1882
1883/* This concatenates the source address with the 32-bit hash of the Host and
1884 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1885 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1886 * on the source address length. The URL hash is stored before the address so
1887 * that in environments where IPv6 is insignificant, truncating the output to
1888 * 8 bytes would still work.
1889 */
1890static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1891{
1892 struct buffer *temp;
1893 struct connection *cli_conn = objt_conn(smp->sess->origin);
1894
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001895 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001896 return 0;
1897
1898 if (!smp_fetch_url32(args, smp, kw, private))
1899 return 0;
1900
1901 temp = get_trash_chunk();
1902 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1903 temp->data += sizeof(unsigned int);
1904
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001905 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001906 case AF_INET:
1907 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001908 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001909 4);
1910 temp->data += 4;
1911 break;
1912 case AF_INET6:
1913 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001914 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001915 16);
1916 temp->data += 16;
1917 break;
1918 default:
1919 return 0;
1920 }
1921
1922 smp->data.u.str = *temp;
1923 smp->data.type = SMP_T_BIN;
1924 return 1;
1925}
1926
1927/************************************************************************/
1928/* Other utility functions */
1929/************************************************************************/
1930
1931/* This function is used to validate the arguments passed to any "hdr" fetch
1932 * keyword. These keywords support an optional positive or negative occurrence
1933 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
1934 * is assumed that the types are already the correct ones. Returns 0 on error,
1935 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
1936 * error message in case of error, that the caller is responsible for freeing.
1937 * The initial location must either be freeable or NULL.
1938 * Note: this function's pointer is checked from Lua.
1939 */
1940int val_hdr(struct arg *arg, char **err_msg)
1941{
1942 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
1943 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
1944 return 0;
1945 }
1946 return 1;
1947}
1948
1949/************************************************************************/
1950/* All supported sample fetch keywords must be declared here. */
1951/************************************************************************/
1952
1953/* Note: must not be declared <const> as its list will be overwritten */
1954static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
1955 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1956 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
1957 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
1958
1959 /* capture are allocated and are permanent in the stream */
1960 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
1961
1962 /* retrieve these captures from the HTTP logs */
1963 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1964 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1965 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1966
1967 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
1968 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
1969
1970 /* cookie is valid in both directions (eg: for "stick ...") but cook*
1971 * are only here to match the ACL's name, are request-only and are used
1972 * for ACL compatibility only.
1973 */
1974 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001975 { "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 +02001976 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1977 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1978
1979 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
1980 * only here to match the ACL's name, are request-only and are used for
1981 * ACL compatibility only.
1982 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001983 { "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 +02001984 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
1985 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
1986 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
1987
Christopher Fauleta4063562019-08-02 11:51:37 +02001988 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1989 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1990 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02001991 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
1992 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
1993 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
1994 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
1995 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1996 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
1997
1998 /* HTTP protocol on the request path */
1999 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2000 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2001
2002 /* HTTP version on the request path */
2003 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2004 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2005
2006 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2007 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2008 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2009 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2010
2011 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2012 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2013
2014 /* HTTP version on the response path */
2015 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2016 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2017
2018 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2019 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2020 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2021 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2022
2023 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2024 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2025 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2026 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2027 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2028 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2029 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2030
2031 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2032 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2033 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2034 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2035
2036 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2037 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2038 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2039 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2040 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2041 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2042 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2043
2044 /* scook is valid only on the response and is used for ACL compatibility */
2045 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2046 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2047 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2048 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2049
2050 /* shdr is valid only on the response and is used for ACL compatibility */
2051 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2052 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2053 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2054 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2055
2056 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2057 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2058 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2059 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2060 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2061 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2062 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2063 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2064 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2065 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2066 { /* END */ },
2067}};
2068
Willy Tarreau0108d902018-11-25 19:14:37 +01002069INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002070
2071/*
2072 * Local variables:
2073 * c-indent-level: 8
2074 * c-basic-offset: 8
2075 * End:
2076 */