blob: 6ccd9dd9816ff3b343b834a32a4472f47283f847 [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
Willy Tarreaub2551052020-06-09 09:07:15 +020019#include <haproxy/api.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020020#include <haproxy/arg.h>
Willy Tarreauac13aea2020-06-04 10:36:03 +020021#include <haproxy/auth.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020022#include <haproxy/base64.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020023#include <haproxy/channel.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020024#include <haproxy/chunk.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020025#include <haproxy/connection.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020026#include <haproxy/global.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020027#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020028#include <haproxy/h1_htx.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020029#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020030#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020031#include <haproxy/http_fetch.h>
Willy Tarreau87735332020-06-04 09:08:41 +020032#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020033#include <haproxy/htx.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020034#include <haproxy/obj_type.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020035#include <haproxy/pool.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020036#include <haproxy/sample.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/stream.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020038#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020039#include <haproxy/version.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020040
Willy Tarreau79e57332018-10-02 16:01:16 +020041
42/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Fauletef453ed2018-10-24 21:39:27 +020043static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
Richard Russo458eafb2019-07-31 11:45:56 -070044/* this is used to convert raw connection buffers to htx */
45static THREAD_LOCAL struct buffer static_raw_htx_chunk;
46static THREAD_LOCAL char *static_raw_htx_buf;
Christopher Fauletef453ed2018-10-24 21:39:27 +020047
Christopher Faulet89dc4992019-04-17 12:02:59 +020048#define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL)
49#define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL)
Willy Tarreau79e57332018-10-02 16:01:16 +020050
Richard Russo458eafb2019-07-31 11:45:56 -070051/* This function returns the static htx chunk, where raw connections get
52 * converted to HTX as needed for samplxsing.
53 */
54struct buffer *get_raw_htx_chunk(void)
55{
56 chunk_reset(&static_raw_htx_chunk);
57 return &static_raw_htx_chunk;
58}
59
60static int alloc_raw_htx_chunk_per_thread()
61{
62 static_raw_htx_buf = malloc(global.tune.bufsize);
63 if (!static_raw_htx_buf)
64 return 0;
65 chunk_init(&static_raw_htx_chunk, static_raw_htx_buf, global.tune.bufsize);
66 return 1;
67}
68
69static void free_raw_htx_chunk_per_thread()
70{
Willy Tarreau61cfdf42021-02-20 10:46:51 +010071 ha_free(&static_raw_htx_buf);
Richard Russo458eafb2019-07-31 11:45:56 -070072}
73
74REGISTER_PER_THREAD_ALLOC(alloc_raw_htx_chunk_per_thread);
75REGISTER_PER_THREAD_FREE(free_raw_htx_chunk_per_thread);
76
Willy Tarreau79e57332018-10-02 16:01:16 +020077/*
78 * Returns the data from Authorization header. Function may be called more
79 * than once so data is stored in txn->auth_data. When no header is found
80 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
81 * searching again for something we are unable to find anyway. However, if
82 * the result if valid, the cache is not reused because we would risk to
83 * have the credentials overwritten by another stream in parallel.
Willy Tarreaueae83722020-04-29 11:52:51 +020084 * The caller is responsible for passing a sample with a valid stream/txn,
85 * and a valid htx.
Willy Tarreau79e57332018-10-02 16:01:16 +020086 */
87
Christopher Fauletcd761952019-07-15 13:58:29 +020088static int get_http_auth(struct sample *smp, struct htx *htx)
Willy Tarreau79e57332018-10-02 16:01:16 +020089{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020090 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020091 struct http_txn *txn = s->txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020092 struct http_hdr_ctx ctx = { .blk = NULL };
93 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020094 struct buffer auth_method;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020095 char *p;
Willy Tarreau79e57332018-10-02 16:01:16 +020096 int len;
97
98#ifdef DEBUG_AUTH
99 printf("Auth for stream %p: %d\n", s, txn->auth.method);
100#endif
Willy Tarreau79e57332018-10-02 16:01:16 +0200101 if (txn->auth.method == HTTP_AUTH_WRONG)
102 return 0;
103
104 txn->auth.method = HTTP_AUTH_WRONG;
105
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200106 if (txn->flags & TX_USE_PX_CONN)
107 hdr = ist("Proxy-Authorization");
108 else
109 hdr = ist("Authorization");
Willy Tarreau79e57332018-10-02 16:01:16 +0200110
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200111 ctx.blk = NULL;
112 if (!http_find_header(htx, hdr, &ctx, 0))
113 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200114
Willy Tarreau17254932020-09-02 07:08:47 +0200115 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
116 if (!p || p == ctx.value.ptr) /* if no space was found or if the space is the first character */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200117 return 0;
Willy Tarreau17254932020-09-02 07:08:47 +0200118 len = p - ctx.value.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +0200119
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200120 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
121 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200122
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200123 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200124
125 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
126 struct buffer *http_auth = get_trash_chunk();
127
128 len = base64dec(txn->auth.method_data.area,
129 txn->auth.method_data.data,
130 http_auth->area, global.tune.bufsize - 1);
131
132 if (len < 0)
133 return 0;
134
135
136 http_auth->area[len] = '\0';
137
138 p = strchr(http_auth->area, ':');
139
140 if (!p)
141 return 0;
142
143 txn->auth.user = http_auth->area;
144 *p = '\0';
145 txn->auth.pass = p+1;
146
147 txn->auth.method = HTTP_AUTH_BASIC;
148 return 1;
149 }
150
151 return 0;
152}
153
154/* This function ensures that the prerequisites for an L7 fetch are ready,
155 * which means that a request or response is ready. If some data is missing,
156 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200157 * to extract data from L7. If <vol> is non-null during a prefetch, another
158 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200159 *
160 * The function returns :
161 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
162 * decide whether or not an HTTP message is present ;
163 * NULL if the requested data cannot be fetched or if it is certain that
Willy Tarreaueae83722020-04-29 11:52:51 +0200164 * we'll never have any HTTP message there; this includes null strm or chn.
Willy Tarreaua6d98792020-08-12 14:04:52 +0200165 * NULL if the sample's direction does not match the channel's (i.e. the
166 * function was asked to work on the wrong channel)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200167 * The HTX message if ready
168 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200169struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, struct check *check, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200170{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200171 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200172 struct http_txn *txn = NULL;
173 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200174 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100175 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200176
Willy Tarreaua6d98792020-08-12 14:04:52 +0200177 if (chn &&
178 (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ && (chn->flags & CF_ISRESP)) ||
179 ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES && !(chn->flags & CF_ISRESP))))
180 return 0;
181
Christopher Fauletef453ed2018-10-24 21:39:27 +0200182 /* Note: it is possible that <s> is NULL when called before stream
183 * initialization (eg: tcp-request connection), so this function is the
184 * one responsible for guarding against this case for all HTTP users.
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200185 *
186 * In the health check context, the stream and the channel must be NULL
187 * and <check> must be set. In this case, only the input buffer,
188 * corresponding to the response, is considered. It is the caller
189 * responsibility to provide <check>.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200190 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200191 BUG_ON(check && (s || chn));
192 if (!s || !chn) {
193 if (check) {
194 htx = htxbuf(&check->bi);
195
196 /* Analyse not yet started */
197 if (htx_is_empty(htx) || htx->first == -1)
198 return NULL;
199
200 sl = http_get_stline(htx);
201 if (vol && !sl) {
202 /* The start-line was already forwarded, it is too late to fetch anything */
203 return NULL;
204 }
205 goto end;
206 }
207
Christopher Fauletef453ed2018-10-24 21:39:27 +0200208 return NULL;
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200209 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200210
211 if (!s->txn) {
212 if (unlikely(!http_alloc_txn(s)))
213 return NULL; /* not enough memory */
214 http_init_txn(s);
215 txn = s->txn;
216 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200217 txn = s->txn;
218 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200219
Christopher Fauleteca88542019-04-03 10:12:42 +0200220 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200221 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200222
Christopher Faulet89dc4992019-04-17 12:02:59 +0200223 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
224 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200225
Christopher Faulet89dc4992019-04-17 12:02:59 +0200226 if (msg->msg_state < HTTP_MSG_BODY) {
227 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200228 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200229 /* Parsing is done by the mux, just wait */
230 smp->flags |= SMP_F_MAY_CHANGE;
231 return NULL;
232 }
233 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200234 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200235 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200236 /* The start-line was already forwarded, it is too late to fetch anything */
237 return NULL;
238 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200239 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200240 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200241 struct buffer *buf;
242 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200243 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200244 union h1_sl h1sl;
245 unsigned int flags = HTX_FL_NONE;
246 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200247
Christopher Faulet89dc4992019-04-17 12:02:59 +0200248 /* no HTTP fetch on the response in TCP mode */
249 if (chn->flags & CF_ISRESP)
250 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200251
Christopher Faulet89dc4992019-04-17 12:02:59 +0200252 /* Now we are working on the request only */
253 buf = &chn->buf;
254 if (b_head(buf) + b_data(buf) > b_wrap(buf))
255 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200256
Christopher Faulet89dc4992019-04-17 12:02:59 +0200257 h1m_init_req(&h1m);
258 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
259 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
260 if (ret <= 0) {
261 /* Invalid or too big*/
262 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200263 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100264
Christopher Faulet89dc4992019-04-17 12:02:59 +0200265 /* wait for a full request */
266 smp->flags |= SMP_F_MAY_CHANGE;
267 return NULL;
268 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100269
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500270 /* OK we just got a valid HTTP message. We have to convert it
Christopher Faulet89dc4992019-04-17 12:02:59 +0200271 * into an HTX message.
272 */
273 if (unlikely(h1sl.rq.v.len == 0)) {
274 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
275 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200276 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200277 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200278 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200279
280 /* Set HTX start-line flags */
281 if (h1m.flags & H1_MF_VER_11)
282 flags |= HTX_SL_F_VER_11;
283 if (h1m.flags & H1_MF_XFER_ENC)
284 flags |= HTX_SL_F_XFER_ENC;
285 flags |= HTX_SL_F_XFER_LEN;
286 if (h1m.flags & H1_MF_CHNK)
287 flags |= HTX_SL_F_CHNK;
288 else if (h1m.flags & H1_MF_CLEN)
289 flags |= HTX_SL_F_CLEN;
290
Richard Russo458eafb2019-07-31 11:45:56 -0700291 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200292 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
293 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200294 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200295 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200296 }
297
298 /* OK we just got a valid HTTP message. If not already done by
299 * HTTP analyzers, we have some minor preparation to perform so
300 * that further checks can rely on HTTP tests.
301 */
302 if (sl && msg->msg_state < HTTP_MSG_BODY) {
303 if (!(chn->flags & CF_ISRESP)) {
304 txn->meth = sl->info.req.meth;
305 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
306 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200307 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200308 else
309 txn->status = sl->info.res.status;
310 if (sl->flags & HTX_SL_F_VER_11)
311 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200312 }
313
314 /* everything's OK */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200315 end:
Christopher Fauletef453ed2018-10-24 21:39:27 +0200316 return htx;
317}
318
Willy Tarreau79e57332018-10-02 16:01:16 +0200319/* This function fetches the method of current HTTP request and stores
320 * it in the global pattern struct as a chunk. There are two possibilities :
321 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
322 * in <len> and <ptr> is NULL ;
323 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
324 * <len> to its length.
325 * This is intended to be used with pat_match_meth() only.
326 */
327static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
328{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200329 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200330 struct http_txn *txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200331 int meth;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200332
Willy Tarreaua6d98792020-08-12 14:04:52 +0200333 txn = smp->strm->txn;
334 if (!txn)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200335 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200336
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200337 meth = txn->meth;
338 smp->data.type = SMP_T_METH;
339 smp->data.u.meth.meth = meth;
340 if (meth == HTTP_METH_OTHER) {
Willy Tarreaua6d98792020-08-12 14:04:52 +0200341 struct htx *htx;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200342 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200343
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200344 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
345 /* ensure the indexes are not affected */
346 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200347 }
Willy Tarreaua6d98792020-08-12 14:04:52 +0200348
349 htx = smp_prefetch_htx(smp, chn, NULL, 0);
350 if (!htx)
351 return 0;
352
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200353 sl = http_get_stline(htx);
354 smp->flags |= SMP_F_CONST;
355 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
356 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200357 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200358 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200359 return 1;
360}
361
362static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
363{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200364 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200365 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200366 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200367 char *ptr;
368 int len;
369
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200370 if (!htx)
371 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200372
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200373 sl = http_get_stline(htx);
374 len = HTX_SL_REQ_VLEN(sl);
375 ptr = HTX_SL_REQ_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200376
377 while ((len-- > 0) && (*ptr++ != '/'));
378 if (len <= 0)
379 return 0;
380
381 smp->data.type = SMP_T_STR;
382 smp->data.u.str.area = ptr;
383 smp->data.u.str.data = len;
384
385 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
386 return 1;
387}
388
389static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
390{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200391 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200392 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200393 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200394 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200395 char *ptr;
396 int len;
397
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200398 if (!htx)
399 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200400
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200401 sl = http_get_stline(htx);
402 len = HTX_SL_RES_VLEN(sl);
403 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200404
405 while ((len-- > 0) && (*ptr++ != '/'));
406 if (len <= 0)
407 return 0;
408
409 smp->data.type = SMP_T_STR;
410 smp->data.u.str.area = ptr;
411 smp->data.u.str.data = len;
412
413 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
414 return 1;
415}
416
417/* 3. Check on Status Code. We manipulate integers here. */
418static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
419{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200420 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200421 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200422 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200423 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200424 char *ptr;
425 int len;
426
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200427 if (!htx)
428 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200429
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200430 sl = http_get_stline(htx);
431 len = HTX_SL_RES_CLEN(sl);
432 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200433
434 smp->data.type = SMP_T_SINT;
435 smp->data.u.sint = __strl2ui(ptr, len);
436 smp->flags = SMP_F_VOL_1ST;
437 return 1;
438}
439
440static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
441{
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100442 struct ist unique_id;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100443
Willy Tarreau79e57332018-10-02 16:01:16 +0200444 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
445 return 0;
446
Willy Tarreaua1062a42020-04-29 11:50:38 +0200447 if (!smp->strm)
448 return 0;
449
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100450 unique_id = stream_generate_unique_id(smp->strm, &smp->sess->fe->format_unique_id);
451 if (!isttest(unique_id))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100452 return 0;
453
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100454 smp->data.u.str.area = smp->strm->unique_id.ptr;
455 smp->data.u.str.data = smp->strm->unique_id.len;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100456 smp->data.type = SMP_T_STR;
Willy Tarreau79e57332018-10-02 16:01:16 +0200457 smp->flags = SMP_F_CONST;
458 return 1;
459}
460
461/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800462 * empty line which separes headers from the body. This is useful
463 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200464 */
465static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
466{
Christopher Faulete596d182020-05-05 17:46:34 +0200467 /* possible keywords: req.hdrs, res.hdrs */
468 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200469 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200470 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200471 struct buffer *temp;
472 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200473
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200474 if (!htx)
475 return 0;
476 temp = get_trash_chunk();
477 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
478 struct htx_blk *blk = htx_get_blk(htx, pos);
479 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200480
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200481 if (type == HTX_BLK_HDR) {
482 struct ist n = htx_get_blk_name(htx, blk);
483 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200484
Christopher Faulet53a899b2019-10-08 16:38:42 +0200485 if (!h1_format_htx_hdr(n, v, temp))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200486 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200487 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200488 else if (type == HTX_BLK_EOH) {
489 if (!chunk_memcat(temp, "\r\n", 2))
490 return 0;
491 break;
492 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200493 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200494 smp->data.type = SMP_T_STR;
495 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200496 return 1;
497}
498
499/* Returns the header request in a length/value encoded format.
500 * This is useful for exchanges with the SPOE.
501 *
502 * A "length value" is a multibyte code encoding numbers. It uses the
503 * SPOE format. The encoding is the following:
504 *
505 * Each couple "header name" / "header value" is composed
506 * like this:
507 * "length value" "header name bytes"
508 * "length value" "header value bytes"
509 * When the last header is reached, the header name and the header
510 * value are empty. Their length are 0
511 */
512static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
513{
Christopher Faulete596d182020-05-05 17:46:34 +0200514 /* possible keywords: req.hdrs_bin, res.hdrs_bin */
515 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200516 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200517 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200518 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200519 char *p, *end;
520 int32_t pos;
521 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200522
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200523 if (!htx)
524 return 0;
525 temp = get_trash_chunk();
526 p = temp->area;
527 end = temp->area + temp->size;
528 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
529 struct htx_blk *blk = htx_get_blk(htx, pos);
530 enum htx_blk_type type = htx_get_blk_type(blk);
531 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200532
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200533 if (type == HTX_BLK_HDR) {
534 n = htx_get_blk_name(htx,blk);
535 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200536
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200537 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200538 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200539 if (ret == -1)
540 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200541 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200542 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200543 memcpy(p, n.ptr, n.len);
544 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200545
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200546 /* encode the header value. */
547 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200548 if (ret == -1)
549 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200550 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200551 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200552 memcpy(p, v.ptr, v.len);
553 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200554
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200555 }
556 else if (type == HTX_BLK_EOH) {
557 /* encode the end of the header list with empty
558 * header name and header value.
559 */
560 ret = encode_varint(0, &p, end);
561 if (ret == -1)
562 return 0;
563 ret = encode_varint(0, &p, end);
564 if (ret == -1)
565 return 0;
566 break;
567 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200568 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200569
570 /* Initialise sample data which will be filled. */
571 smp->data.type = SMP_T_BIN;
572 smp->data.u.str.area = temp->area;
573 smp->data.u.str.data = p - temp->area;
574 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200575 return 1;
576}
577
578/* returns the longest available part of the body. This requires that the body
579 * has been waited for using http-buffer-request.
580 */
581static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
582{
Christopher Faulete596d182020-05-05 17:46:34 +0200583 /* possible keywords: req.body, res.body */
584 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200585 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200586 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200587 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200588 int32_t pos;
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100589 int finished = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200590
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200591 if (!htx)
592 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200593
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200594 temp = get_trash_chunk();
595 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
596 struct htx_blk *blk = htx_get_blk(htx, pos);
597 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200598
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100599 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) {
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100600 finished = 1;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200601 break;
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100602 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200603 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +0200604 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200605 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200606 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200607 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200608
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200609 smp->data.type = SMP_T_BIN;
610 smp->data.u.str = *temp;
611 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200612
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100613 if (!finished && (check || (chn && !channel_full(chn, global.tune.maxrewrite) &&
614 !(chn->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)))))
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200615 smp->flags |= SMP_F_MAY_CHANGE;
616
Willy Tarreau79e57332018-10-02 16:01:16 +0200617 return 1;
618}
619
620
621/* returns the available length of the body. This requires that the body
622 * has been waited for using http-buffer-request.
623 */
624static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
625{
Christopher Faulete596d182020-05-05 17:46:34 +0200626 /* possible keywords: req.body_len, res.body_len */
627 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200628 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200629 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200630 int32_t pos;
631 unsigned long long len = 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100632
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200633 if (!htx)
634 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100635
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200636 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
637 struct htx_blk *blk = htx_get_blk(htx, pos);
638 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100639
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100640 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200641 break;
642 if (type == HTX_BLK_DATA)
643 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200644 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200645
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200646 smp->data.type = SMP_T_SINT;
647 smp->data.u.sint = len;
648 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200649 return 1;
650}
651
652
653/* returns the advertised length of the body, or the advertised size of the
654 * chunks available in the buffer. This requires that the body has been waited
655 * for using http-buffer-request.
656 */
657static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
658{
Christopher Faulete596d182020-05-05 17:46:34 +0200659 /* possible keywords: req.body_size, res.body_size */
660 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200661 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200662 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200663 int32_t pos;
664 unsigned long long len = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200665
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200666 if (!htx)
667 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100668
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200669 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
670 struct htx_blk *blk = htx_get_blk(htx, pos);
671 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100672
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100673 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200674 break;
675 if (type == HTX_BLK_DATA)
676 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200677 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200678 if (htx->extra != ULLONG_MAX)
679 len += htx->extra;
Willy Tarreau79e57332018-10-02 16:01:16 +0200680
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200681 smp->data.type = SMP_T_SINT;
682 smp->data.u.sint = len;
683 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200684 return 1;
685}
686
687
688/* 4. Check on URL/URI. A pointer to the URI is stored. */
689static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
690{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200691 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200692 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200693 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200694
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200695 if (!htx)
696 return 0;
697 sl = http_get_stline(htx);
698 smp->data.type = SMP_T_STR;
699 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
700 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
701 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200702 return 1;
703}
704
705static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
706{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200707 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200708 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200709 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200710 struct sockaddr_storage addr;
711
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200712 if (!htx)
713 return 0;
714 sl = http_get_stline(htx);
715 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +0200716
Willy Tarreau79e57332018-10-02 16:01:16 +0200717 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
718 return 0;
719
720 smp->data.type = SMP_T_IPV4;
721 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
722 smp->flags = 0;
723 return 1;
724}
725
726static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
727{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200728 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200729 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200730 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200731 struct sockaddr_storage addr;
732
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200733 if (!htx)
734 return 0;
735 sl = http_get_stline(htx);
736 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200737
Willy Tarreau79e57332018-10-02 16:01:16 +0200738 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
739 return 0;
740
741 smp->data.type = SMP_T_SINT;
742 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
743 smp->flags = 0;
744 return 1;
745}
746
747/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
748 * Accepts an optional argument of type string containing the header field name,
749 * and an optional argument of type signed or unsigned integer to request an
750 * explicit occurrence of the header. Note that in the event of a missing name,
751 * headers are considered from the first one. It does not stop on commas and
752 * returns full lines instead (useful for User-Agent or Date for example).
753 */
754static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
755{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200756 /* possible keywords: req.fhdr, res.fhdr */
757 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200758 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200759 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200760 struct http_hdr_ctx *ctx = smp->ctx.a[0];
761 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200762 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200763
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200764 if (!ctx) {
765 /* first call */
766 ctx = &static_http_hdr_ctx;
767 ctx->blk = NULL;
768 smp->ctx.a[0] = ctx;
769 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200770
Christopher Faulet623af932021-01-29 11:22:15 +0100771 if (args[0].type != ARGT_STR)
772 return 0;
773 name.ptr = args[0].data.str.area;
774 name.len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +0200775
Christopher Faulet623af932021-01-29 11:22:15 +0100776 if (args[1].type == ARGT_SINT)
777 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200778
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200779 if (!htx)
780 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200781
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200782 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
783 /* search for header from the beginning */
784 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200785
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200786 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
787 /* no explicit occurrence and single fetch => last header by default */
788 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200789
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200790 if (!occ)
791 /* prepare to report multiple occurrences for ACL fetches */
792 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200793
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200794 smp->data.type = SMP_T_STR;
795 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
796 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
797 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200798 smp->flags &= ~SMP_F_NOT_LAST;
799 return 0;
800}
801
802/* 6. Check on HTTP header count. The number of occurrences is returned.
803 * Accepts exactly 1 argument of type string. It does not stop on commas and
804 * returns full lines instead (useful for User-Agent or Date for example).
805 */
806static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
807{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200808 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
809 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200810 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200811 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200812 struct http_hdr_ctx ctx;
813 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200814 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200815
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200816 if (!htx)
817 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200818
Christopher Faulet623af932021-01-29 11:22:15 +0100819 if (args->type == ARGT_STR) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200820 name.ptr = args->data.str.area;
821 name.len = args->data.str.data;
822 } else {
823 name.ptr = NULL;
824 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200825 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200826
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200827 ctx.blk = NULL;
828 cnt = 0;
829 while (http_find_header(htx, name, &ctx, 1))
830 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200831 smp->data.type = SMP_T_SINT;
832 smp->data.u.sint = cnt;
833 smp->flags = SMP_F_VOL_HDR;
834 return 1;
835}
836
837static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
838{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200839 /* possible keywords: req.hdr_names, res.hdr_names */
840 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200841 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200842 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200843 struct buffer *temp;
844 char del = ',';
845
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200846 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200847
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200848 if (!htx)
849 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200850
Christopher Faulet623af932021-01-29 11:22:15 +0100851 if (args->type == ARGT_STR)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200852 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200853
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200854 temp = get_trash_chunk();
855 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
856 struct htx_blk *blk = htx_get_blk(htx, pos);
857 enum htx_blk_type type = htx_get_blk_type(blk);
858 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200859
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200860 if (type == HTX_BLK_EOH)
861 break;
862 if (type != HTX_BLK_HDR)
863 continue;
864 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200865
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200866 if (temp->data)
867 temp->area[temp->data++] = del;
868 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200869 }
870
871 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200872 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200873 smp->flags = SMP_F_VOL_HDR;
874 return 1;
875}
876
877/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
878 * Accepts an optional argument of type string containing the header field name,
879 * and an optional argument of type signed or unsigned integer to request an
880 * explicit occurrence of the header. Note that in the event of a missing name,
881 * headers are considered from the first one.
882 */
883static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
884{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200885 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
886 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200887 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200888 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200889 struct http_hdr_ctx *ctx = smp->ctx.a[0];
890 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200891 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200892
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200893 if (!ctx) {
894 /* first call */
895 ctx = &static_http_hdr_ctx;
896 ctx->blk = NULL;
897 smp->ctx.a[0] = ctx;
898 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200899
Christopher Faulet623af932021-01-29 11:22:15 +0100900 if (args[0].type != ARGT_STR)
901 return 0;
902 name.ptr = args[0].data.str.area;
903 name.len = args[0].data.str.data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200904
Christopher Faulet623af932021-01-29 11:22:15 +0100905 if (args[1].type == ARGT_SINT)
906 occ = args[1].data.sint;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200907
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200908 if (!htx)
909 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200910
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200911 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
912 /* search for header from the beginning */
913 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200914
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200915 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
916 /* no explicit occurrence and single fetch => last header by default */
917 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200918
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200919 if (!occ)
920 /* prepare to report multiple occurrences for ACL fetches */
921 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200922
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200923 smp->data.type = SMP_T_STR;
924 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
925 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
926 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200927
928 smp->flags &= ~SMP_F_NOT_LAST;
929 return 0;
930}
931
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200932/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
933 * the right channel. So instead of duplicating the code, we just change the
934 * keyword and then fallback on smp_fetch_hdr().
935 */
936static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
937{
938 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
939 return smp_fetch_hdr(args, smp, kw, private);
940}
941
Willy Tarreau79e57332018-10-02 16:01:16 +0200942/* 6. Check on HTTP header count. The number of occurrences is returned.
943 * Accepts exactly 1 argument of type string.
944 */
945static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
946{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200947 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
948 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200949 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200950 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200951 struct http_hdr_ctx ctx;
952 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200953 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200954
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200955 if (!htx)
956 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200957
Christopher Faulet623af932021-01-29 11:22:15 +0100958 if (args->type == ARGT_STR) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200959 name.ptr = args->data.str.area;
960 name.len = args->data.str.data;
961 } else {
962 name.ptr = NULL;
963 name.len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200964 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200965
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200966 ctx.blk = NULL;
967 cnt = 0;
968 while (http_find_header(htx, name, &ctx, 0))
969 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200970
971 smp->data.type = SMP_T_SINT;
972 smp->data.u.sint = cnt;
973 smp->flags = SMP_F_VOL_HDR;
974 return 1;
975}
976
977/* Fetch an HTTP header's integer value. The integer value is returned. It
978 * takes a mandatory argument of type string and an optional one of type int
979 * to designate a specific occurrence. It returns an unsigned integer, which
980 * may or may not be appropriate for everything.
981 */
982static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
983{
984 int ret = smp_fetch_hdr(args, smp, kw, private);
985
986 if (ret > 0) {
987 smp->data.type = SMP_T_SINT;
988 smp->data.u.sint = strl2ic(smp->data.u.str.area,
989 smp->data.u.str.data);
990 }
991
992 return ret;
993}
994
995/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
996 * and an optional one of type int to designate a specific occurrence.
997 * It returns an IPv4 or IPv6 address.
998 */
999static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1000{
1001 int ret;
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001002 struct buffer *temp = get_trash_chunk();
Willy Tarreau79e57332018-10-02 16:01:16 +02001003
1004 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001005 if (smp->data.u.str.data < temp->size - 1) {
1006 memcpy(temp->area, smp->data.u.str.area,
1007 smp->data.u.str.data);
1008 temp->area[smp->data.u.str.data] = '\0';
1009 if (url2ipv4((char *) temp->area, &smp->data.u.ipv4)) {
1010 smp->data.type = SMP_T_IPV4;
1011 break;
1012 } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1013 smp->data.type = SMP_T_IPV6;
1014 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001015 }
1016 }
1017
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001018 /* if the header doesn't match an IP address, fetch next one */
1019 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001020 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001021 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001022 return ret;
1023}
Willy Tarreau79e57332018-10-02 16:01:16 +02001024
Christopher Faulete720c322020-09-02 17:25:18 +02001025/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at the
1026 * first '/' after the possible hostname. It ends before the possible '?' except
1027 * for 'pathq' keyword.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001028 */
1029static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1030{
1031 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001032 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001033 struct htx_sl *sl;
1034 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001035
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001036 if (!htx)
1037 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001038
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001039 sl = http_get_stline(htx);
Christopher Faulete720c322020-09-02 17:25:18 +02001040 path = http_get_path(htx_sl_req_uri(sl));
1041
Yves Lafonb4d37082021-02-11 11:01:28 +01001042 if (kw[4] == 'q' && (kw[0] == 'p' || kw[0] == 'b')) // pathq or baseq
Christopher Faulete720c322020-09-02 17:25:18 +02001043 path = http_get_path(htx_sl_req_uri(sl));
1044 else
1045 path = iststop(http_get_path(htx_sl_req_uri(sl)), '?');
1046
Tim Duesterhused526372020-03-05 17:56:33 +01001047 if (!isttest(path))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001048 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001049
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001050 /* OK, we got the '/' ! */
1051 smp->data.type = SMP_T_STR;
1052 smp->data.u.str.area = path.ptr;
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001053 smp->data.u.str.data = path.len;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001054 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001055 return 1;
1056}
1057
1058/* This produces a concatenation of the first occurrence of the Host header
1059 * followed by the path component if it begins with a slash ('/'). This means
1060 * that '*' will not be added, resulting in exactly the first Host entry.
1061 * If no Host header is found, then the path is returned as-is. The returned
1062 * value is stored in the trash so it does not need to be marked constant.
1063 * The returned sample is of type string.
1064 */
1065static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1066{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001067 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001068 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001069 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001070 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001071 struct http_hdr_ctx ctx;
1072 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001073
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001074 if (!htx)
1075 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001076
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001077 ctx.blk = NULL;
1078 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1079 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001080
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001081 /* OK we have the header value in ctx.value */
1082 temp = get_trash_chunk();
1083 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001084
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001085 /* now retrieve the path */
1086 sl = http_get_stline(htx);
1087 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001088 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001089 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001090
Yves Lafonb4d37082021-02-11 11:01:28 +01001091 if (kw[4] == 'q' && kw[0] == 'b') { // baseq
1092 len = path.len;
1093 } else {
1094 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1095 ;
1096 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001097
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001098 if (len && *(path.ptr) == '/')
1099 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001100 }
1101
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001102 smp->data.type = SMP_T_STR;
1103 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001104 smp->flags = SMP_F_VOL_1ST;
1105 return 1;
1106}
1107
1108/* This produces a 32-bit hash of the concatenation of the first occurrence of
1109 * the Host header followed by the path component if it begins with a slash ('/').
1110 * This means that '*' will not be added, resulting in exactly the first Host
1111 * entry. If no Host header is found, then the path is used. The resulting value
1112 * is hashed using the path hash followed by a full avalanche hash and provides a
1113 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1114 * high-traffic sites without having to store whole paths.
1115 */
1116static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1117{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001118 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001119 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001120 struct htx_sl *sl;
1121 struct http_hdr_ctx ctx;
1122 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001123 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001124
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001125 if (!htx)
1126 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001127
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001128 ctx.blk = NULL;
1129 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1130 /* OK we have the header value in ctx.value */
1131 while (ctx.value.len--)
1132 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001133 }
1134
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001135 /* now retrieve the path */
1136 sl = http_get_stline(htx);
1137 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001138 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001139 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001140
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001141 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1142 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001143
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001144 if (len && *(path.ptr) == '/') {
1145 while (len--)
1146 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001147 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001148 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001149
Willy Tarreau79e57332018-10-02 16:01:16 +02001150 hash = full_hash(hash);
1151
1152 smp->data.type = SMP_T_SINT;
1153 smp->data.u.sint = hash;
1154 smp->flags = SMP_F_VOL_1ST;
1155 return 1;
1156}
1157
1158/* This concatenates the source address with the 32-bit hash of the Host and
1159 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1160 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1161 * on the source address length. The path hash is stored before the address so
1162 * that in environments where IPv6 is insignificant, truncating the output to
1163 * 8 bytes would still work.
1164 */
1165static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1166{
1167 struct buffer *temp;
1168 struct connection *cli_conn = objt_conn(smp->sess->origin);
1169
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001170 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001171 return 0;
1172
1173 if (!smp_fetch_base32(args, smp, kw, private))
1174 return 0;
1175
1176 temp = get_trash_chunk();
1177 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1178 temp->data += sizeof(unsigned int);
1179
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001180 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001181 case AF_INET:
1182 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001183 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001184 4);
1185 temp->data += 4;
1186 break;
1187 case AF_INET6:
1188 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001189 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001190 16);
1191 temp->data += 16;
1192 break;
1193 default:
1194 return 0;
1195 }
1196
1197 smp->data.u.str = *temp;
1198 smp->data.type = SMP_T_BIN;
1199 return 1;
1200}
1201
1202/* Extracts the query string, which comes after the question mark '?'. If no
1203 * question mark is found, nothing is returned. Otherwise it returns a sample
1204 * of type string carrying the whole query string.
1205 */
1206static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1207{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001208 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001209 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001210 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001211 char *ptr, *end;
1212
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001213 if (!htx)
1214 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001215
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001216 sl = http_get_stline(htx);
1217 ptr = HTX_SL_REQ_UPTR(sl);
1218 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001219
1220 /* look up the '?' */
1221 do {
1222 if (ptr == end)
1223 return 0;
1224 } while (*ptr++ != '?');
1225
1226 smp->data.type = SMP_T_STR;
1227 smp->data.u.str.area = ptr;
1228 smp->data.u.str.data = end - ptr;
1229 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1230 return 1;
1231}
1232
1233static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1234{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001235 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001236 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001237
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001238 if (!htx)
1239 return 0;
1240 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001241 smp->data.u.sint = 1;
1242 return 1;
1243}
1244
1245/* return a valid test if the current request is the first one on the connection */
1246static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1247{
Willy Tarreau79512b62020-04-29 11:52:13 +02001248 if (!smp->strm)
1249 return 0;
1250
Willy Tarreau79e57332018-10-02 16:01:16 +02001251 smp->data.type = SMP_T_BOOL;
1252 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1253 return 1;
1254}
1255
Christopher Fauleta4063562019-08-02 11:51:37 +02001256/* Fetch the authentication method if there is an Authorization header. It
1257 * relies on get_http_auth()
1258 */
1259static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1260{
1261 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001262 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001263 struct http_txn *txn;
1264
1265 if (!htx)
1266 return 0;
1267
1268 txn = smp->strm->txn;
1269 if (!get_http_auth(smp, htx))
1270 return 0;
1271
1272 switch (txn->auth.method) {
1273 case HTTP_AUTH_BASIC:
1274 smp->data.u.str.area = "Basic";
1275 smp->data.u.str.data = 5;
1276 break;
1277 case HTTP_AUTH_DIGEST:
1278 /* Unexpected because not supported */
1279 smp->data.u.str.area = "Digest";
1280 smp->data.u.str.data = 6;
1281 break;
1282 default:
1283 return 0;
1284 }
1285
1286 smp->data.type = SMP_T_STR;
1287 smp->flags = SMP_F_CONST;
1288 return 1;
1289}
1290
1291/* Fetch the user supplied if there is an Authorization header. It relies on
1292 * get_http_auth()
1293 */
1294static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1295{
1296 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001297 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001298 struct http_txn *txn;
1299
1300 if (!htx)
1301 return 0;
1302
1303 txn = smp->strm->txn;
1304 if (!get_http_auth(smp, htx))
1305 return 0;
1306
1307 smp->data.type = SMP_T_STR;
1308 smp->data.u.str.area = txn->auth.user;
1309 smp->data.u.str.data = strlen(txn->auth.user);
1310 smp->flags = SMP_F_CONST;
1311 return 1;
1312}
1313
1314/* Fetch the password supplied if there is an Authorization header. It relies on
1315 * get_http_auth()
1316 */
1317static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1318{
1319 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001320 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001321 struct http_txn *txn;
1322
1323 if (!htx)
1324 return 0;
1325
1326 txn = smp->strm->txn;
1327 if (!get_http_auth(smp, htx))
1328 return 0;
1329
1330 smp->data.type = SMP_T_STR;
1331 smp->data.u.str.area = txn->auth.pass;
1332 smp->data.u.str.data = strlen(txn->auth.pass);
1333 smp->flags = SMP_F_CONST;
1334 return 1;
1335}
1336
Willy Tarreau79e57332018-10-02 16:01:16 +02001337/* Accepts exactly 1 argument of type userlist */
1338static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1339{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001340 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001341 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001342
Christopher Faulet623af932021-01-29 11:22:15 +01001343 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001344 return 0;
1345
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001346 if (!htx)
1347 return 0;
1348 if (!get_http_auth(smp, htx))
1349 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001350
1351 smp->data.type = SMP_T_BOOL;
1352 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001353 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001354 return 1;
1355}
1356
1357/* Accepts exactly 1 argument of type userlist */
1358static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1359{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001360 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001361 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001362
Christopher Faulet623af932021-01-29 11:22:15 +01001363 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001364 return 0;
1365
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001366 if (!htx)
1367 return 0;
1368 if (!get_http_auth(smp, htx))
1369 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001370
Willy Tarreau79e57332018-10-02 16:01:16 +02001371 /* if the user does not belong to the userlist or has a wrong password,
1372 * report that it unconditionally does not match. Otherwise we return
1373 * a string containing the username.
1374 */
1375 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1376 smp->strm->txn->auth.pass))
1377 return 0;
1378
1379 /* pat_match_auth() will need the user list */
1380 smp->ctx.a[0] = args->data.usr;
1381
1382 smp->data.type = SMP_T_STR;
1383 smp->flags = SMP_F_CONST;
1384 smp->data.u.str.area = smp->strm->txn->auth.user;
1385 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1386
1387 return 1;
1388}
1389
1390/* Fetch a captured HTTP request header. The index is the position of
1391 * the "capture" option in the configuration file
1392 */
1393static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1394{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001395 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001396 int idx;
1397
Christopher Faulet623af932021-01-29 11:22:15 +01001398 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001399 return 0;
1400
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001401 if (!smp->strm)
1402 return 0;
1403
1404 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001405 idx = args->data.sint;
1406
1407 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1408 return 0;
1409
1410 smp->data.type = SMP_T_STR;
1411 smp->flags |= SMP_F_CONST;
1412 smp->data.u.str.area = smp->strm->req_cap[idx];
1413 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1414
1415 return 1;
1416}
1417
1418/* Fetch a captured HTTP response header. The index is the position of
1419 * the "capture" option in the configuration file
1420 */
1421static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1422{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001423 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001424 int idx;
1425
Christopher Faulet623af932021-01-29 11:22:15 +01001426 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001427 return 0;
1428
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001429 if (!smp->strm)
1430 return 0;
1431
1432 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001433 idx = args->data.sint;
1434
1435 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1436 return 0;
1437
1438 smp->data.type = SMP_T_STR;
1439 smp->flags |= SMP_F_CONST;
1440 smp->data.u.str.area = smp->strm->res_cap[idx];
1441 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1442
1443 return 1;
1444}
1445
1446/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1447static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1448{
1449 struct buffer *temp;
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001450 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001451 char *ptr;
1452
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001453 if (!smp->strm)
1454 return 0;
1455
1456 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001457 if (!txn || !txn->uri)
1458 return 0;
1459
1460 ptr = txn->uri;
1461
1462 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1463 ptr++;
1464
1465 temp = get_trash_chunk();
1466 temp->area = txn->uri;
1467 temp->data = ptr - txn->uri;
1468 smp->data.u.str = *temp;
1469 smp->data.type = SMP_T_STR;
1470 smp->flags = SMP_F_CONST;
1471
1472 return 1;
1473
1474}
1475
1476/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1477static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1478{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001479 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001480 struct ist path;
1481 const char *ptr;
1482
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001483 if (!smp->strm)
1484 return 0;
1485
1486 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001487 if (!txn || !txn->uri)
1488 return 0;
1489
1490 ptr = txn->uri;
1491
1492 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1493 ptr++;
1494
1495 if (!*ptr)
1496 return 0;
1497
Christopher Faulet78337bb2018-11-15 14:35:18 +01001498 /* skip the first space and find space after URI */
1499 path = ist2(++ptr, 0);
1500 while (*ptr != ' ' && *ptr != '\0')
1501 ptr++;
1502 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001503
Christopher Faulet78337bb2018-11-15 14:35:18 +01001504 path = http_get_path(path);
Tim Duesterhused526372020-03-05 17:56:33 +01001505 if (!isttest(path))
Willy Tarreau79e57332018-10-02 16:01:16 +02001506 return 0;
1507
1508 smp->data.u.str.area = path.ptr;
1509 smp->data.u.str.data = path.len;
1510 smp->data.type = SMP_T_STR;
1511 smp->flags = SMP_F_CONST;
1512
1513 return 1;
1514}
1515
1516/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1517 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1518 */
1519static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1520{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001521 struct http_txn *txn;
1522
1523 if (!smp->strm)
1524 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001525
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001526 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001527 if (!txn || txn->req.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001528 return 0;
1529
1530 if (txn->req.flags & HTTP_MSGF_VER_11)
1531 smp->data.u.str.area = "HTTP/1.1";
1532 else
1533 smp->data.u.str.area = "HTTP/1.0";
1534
1535 smp->data.u.str.data = 8;
1536 smp->data.type = SMP_T_STR;
1537 smp->flags = SMP_F_CONST;
1538 return 1;
1539
1540}
1541
1542/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1543 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1544 */
1545static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1546{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001547 struct http_txn *txn;
1548
1549 if (!smp->strm)
1550 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001551
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001552 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001553 if (!txn || txn->rsp.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001554 return 0;
1555
1556 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1557 smp->data.u.str.area = "HTTP/1.1";
1558 else
1559 smp->data.u.str.area = "HTTP/1.0";
1560
1561 smp->data.u.str.data = 8;
1562 smp->data.type = SMP_T_STR;
1563 smp->flags = SMP_F_CONST;
1564 return 1;
1565
1566}
1567
1568/* Iterate over all cookies present in a message. The context is stored in
1569 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1570 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1571 * the direction, multiple cookies may be parsed on the same line or not.
Maciej Zdebdea7c202020-11-13 09:38:06 +00001572 * If provided, the searched cookie name is in args, in args->data.str. If
1573 * the input options indicate that no iterating is desired, then only last
1574 * value is fetched if any. If no cookie name is provided, the first cookie
1575 * value found is fetched. The returned sample is of type CSTR. Can be used
1576 * to parse cookies in other files.
Willy Tarreau79e57332018-10-02 16:01:16 +02001577 */
1578static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1579{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001580 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1581 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001582 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001583 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001584 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1585 struct ist hdr;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001586 char *cook = NULL;
1587 size_t cook_l = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001588 int found = 0;
1589
Christopher Faulet623af932021-01-29 11:22:15 +01001590 if (args->type == ARGT_STR) {
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001591 cook = args->data.str.area;
1592 cook_l = args->data.str.data;
1593 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001594
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001595 if (!ctx) {
1596 /* first call */
1597 ctx = &static_http_hdr_ctx;
1598 ctx->blk = NULL;
1599 smp->ctx.a[2] = ctx;
1600 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001601
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001602 if (!htx)
1603 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001604
Christopher Faulet16032ab2020-04-30 11:30:00 +02001605 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001606
Maciej Zdebdea7c202020-11-13 09:38:06 +00001607 /* OK so basically here, either we want only one value or we want to
1608 * iterate over all of them and we fetch the next one. In this last case
1609 * SMP_OPT_ITERATE option is set.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001610 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001611
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001612 if (!(smp->flags & SMP_F_NOT_LAST)) {
1613 /* search for the header from the beginning, we must first initialize
1614 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001615 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001616 smp->ctx.a[0] = NULL;
1617 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001618 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001619
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001620 smp->flags |= SMP_F_VOL_HDR;
1621 while (1) {
1622 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1623 if (!smp->ctx.a[0]) {
1624 if (!http_find_header(htx, hdr, ctx, 0))
1625 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001626
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001627 if (ctx->value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001628 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001629
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001630 smp->ctx.a[0] = ctx->value.ptr;
1631 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001632 }
1633
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001634 smp->data.type = SMP_T_STR;
1635 smp->flags |= SMP_F_CONST;
1636 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001637 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001638 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1639 &smp->data.u.str.area,
1640 &smp->data.u.str.data);
1641 if (smp->ctx.a[0]) {
1642 found = 1;
Maciej Zdebdea7c202020-11-13 09:38:06 +00001643 if (smp->opt & SMP_OPT_ITERATE) {
1644 /* iterate on cookie value */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001645 smp->flags |= SMP_F_NOT_LAST;
1646 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001647 }
Maciej Zdebdea7c202020-11-13 09:38:06 +00001648 if (args->data.str.data == 0) {
1649 /* No cookie name, first occurrence returned */
1650 break;
1651 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001652 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001653 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001654 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001655
Willy Tarreau79e57332018-10-02 16:01:16 +02001656 /* all cookie headers and values were scanned. If we're looking for the
1657 * last occurrence, we may return it now.
1658 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001659 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001660 smp->flags &= ~SMP_F_NOT_LAST;
1661 return found;
1662}
1663
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001664/* Same than smp_fetch_cookie() but only relies on the sample direction to
1665 * choose the right channel. So instead of duplicating the code, we just change
1666 * the keyword and then fallback on smp_fetch_cookie().
1667 */
1668static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1669{
1670 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1671 return smp_fetch_cookie(args, smp, kw, private);
1672}
1673
Willy Tarreau79e57332018-10-02 16:01:16 +02001674/* Iterate over all cookies present in a request to count how many occurrences
1675 * match the name in args and args->data.str.len. If <multi> is non-null, then
1676 * multiple cookies may be parsed on the same line. The returned sample is of
1677 * type UINT. Accepts exactly 1 argument of type string.
1678 */
1679static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1680{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001681 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1682 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001683 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001684 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001685 struct http_hdr_ctx ctx;
1686 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001687 char *val_beg, *val_end;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001688 char *cook = NULL;
1689 size_t cook_l = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001690 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001691
Christopher Faulet623af932021-01-29 11:22:15 +01001692 if (args->type == ARGT_STR){
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001693 cook = args->data.str.area;
1694 cook_l = args->data.str.data;
1695 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001696
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001697 if (!htx)
1698 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001699
Christopher Faulet16032ab2020-04-30 11:30:00 +02001700 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001701
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001702 val_end = val_beg = NULL;
1703 ctx.blk = NULL;
1704 cnt = 0;
1705 while (1) {
1706 /* Note: val_beg == NULL every time we need to fetch a new header */
1707 if (!val_beg) {
1708 if (!http_find_header(htx, hdr, &ctx, 0))
1709 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001710
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001711 if (ctx.value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001712 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001713
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001714 val_beg = ctx.value.ptr;
1715 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001716 }
1717
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001718 smp->data.type = SMP_T_STR;
1719 smp->flags |= SMP_F_CONST;
1720 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001721 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001722 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1723 &smp->data.u.str.area,
1724 &smp->data.u.str.data))) {
1725 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001726 }
1727 }
1728
1729 smp->data.type = SMP_T_SINT;
1730 smp->data.u.sint = cnt;
1731 smp->flags |= SMP_F_VOL_HDR;
1732 return 1;
1733}
1734
1735/* Fetch an cookie's integer value. The integer value is returned. It
1736 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1737 */
1738static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1739{
1740 int ret = smp_fetch_cookie(args, smp, kw, private);
1741
1742 if (ret > 0) {
1743 smp->data.type = SMP_T_SINT;
1744 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1745 smp->data.u.str.data);
1746 }
1747
1748 return ret;
1749}
1750
1751/************************************************************************/
1752/* The code below is dedicated to sample fetches */
1753/************************************************************************/
1754
1755/* This scans a URL-encoded query string. It takes an optionally wrapping
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001756 * string whose first contiguous chunk has its beginning in ctx->a[0] and end
Willy Tarreau79e57332018-10-02 16:01:16 +02001757 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1758 * pointers are updated for next iteration before leaving.
1759 */
1760static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1761{
1762 const char *vstart, *vend;
1763 struct buffer *temp;
1764 const char **chunks = (const char **)smp->ctx.a;
1765
1766 if (!http_find_next_url_param(chunks, name, name_len,
1767 &vstart, &vend, delim))
1768 return 0;
1769
1770 /* Create sample. If the value is contiguous, return the pointer as CONST,
1771 * if the value is wrapped, copy-it in a buffer.
1772 */
1773 smp->data.type = SMP_T_STR;
1774 if (chunks[2] &&
1775 vstart >= chunks[0] && vstart <= chunks[1] &&
1776 vend >= chunks[2] && vend <= chunks[3]) {
1777 /* Wrapped case. */
1778 temp = get_trash_chunk();
1779 memcpy(temp->area, vstart, chunks[1] - vstart);
1780 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1781 vend - chunks[2]);
1782 smp->data.u.str.area = temp->area;
1783 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1784 } else {
1785 /* Contiguous case. */
1786 smp->data.u.str.area = (char *)vstart;
1787 smp->data.u.str.data = vend - vstart;
1788 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1789 }
1790
1791 /* Update context, check wrapping. */
1792 chunks[0] = vend;
1793 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1794 chunks[1] = chunks[3];
1795 chunks[2] = NULL;
1796 }
1797
1798 if (chunks[0] < chunks[1])
1799 smp->flags |= SMP_F_NOT_LAST;
1800
1801 return 1;
1802}
1803
1804/* This function iterates over each parameter of the query string. It uses
1805 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1806 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1807 * An optional parameter name is passed in args[0], otherwise any parameter is
1808 * considered. It supports an optional delimiter argument for the beginning of
1809 * the string in args[1], which defaults to "?".
1810 */
1811static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1812{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001813 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001814 char delim = '?';
1815 const char *name;
1816 int name_len;
1817
Christopher Faulet623af932021-01-29 11:22:15 +01001818 if ((args[0].type && args[0].type != ARGT_STR) ||
Willy Tarreau79e57332018-10-02 16:01:16 +02001819 (args[1].type && args[1].type != ARGT_STR))
1820 return 0;
1821
1822 name = "";
1823 name_len = 0;
1824 if (args->type == ARGT_STR) {
1825 name = args->data.str.area;
1826 name_len = args->data.str.data;
1827 }
1828
1829 if (args[1].type)
1830 delim = *args[1].data.str.area;
1831
1832 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001833 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001834 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001835
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001836 if (!htx)
1837 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001838
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001839 sl = http_get_stline(htx);
1840 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1841 if (!smp->ctx.a[0])
1842 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001843
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001844 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001845
1846 /* Assume that the context is filled with NULL pointer
1847 * before the first call.
1848 * smp->ctx.a[2] = NULL;
1849 * smp->ctx.a[3] = NULL;
1850 */
1851 }
1852
1853 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1854}
1855
1856/* This function iterates over each parameter of the body. This requires
1857 * that the body has been waited for using http-buffer-request. It uses
1858 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001859 * contiguous part of the body, and optionally ctx->a[2..3] to reference the
Willy Tarreau79e57332018-10-02 16:01:16 +02001860 * optional second part if the body wraps at the end of the buffer. An optional
1861 * parameter name is passed in args[0], otherwise any parameter is considered.
1862 */
1863static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1864{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001865 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001866 const char *name;
1867 int name_len;
1868
Christopher Faulet623af932021-01-29 11:22:15 +01001869 if (args[0].type && args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001870 return 0;
1871
1872 name = "";
1873 name_len = 0;
1874 if (args[0].type == ARGT_STR) {
1875 name = args[0].data.str.area;
1876 name_len = args[0].data.str.data;
1877 }
1878
1879 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulete596d182020-05-05 17:46:34 +02001880 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001881 struct buffer *temp;
1882 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001883
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001884 if (!htx)
1885 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001886
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001887 temp = get_trash_chunk();
1888 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1889 struct htx_blk *blk = htx_get_blk(htx, pos);
1890 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001891
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001892 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001893 break;
1894 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001895 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001896 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001897 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001898 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001899
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001900 smp->ctx.a[0] = temp->area;
1901 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001902
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001903 /* Assume that the context is filled with NULL pointer
1904 * before the first call.
1905 * smp->ctx.a[2] = NULL;
1906 * smp->ctx.a[3] = NULL;
1907 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001908
Willy Tarreau79e57332018-10-02 16:01:16 +02001909 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001910
Willy Tarreau79e57332018-10-02 16:01:16 +02001911 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1912}
1913
1914/* Return the signed integer value for the specified url parameter (see url_param
1915 * above).
1916 */
1917static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1918{
1919 int ret = smp_fetch_url_param(args, smp, kw, private);
1920
1921 if (ret > 0) {
1922 smp->data.type = SMP_T_SINT;
1923 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1924 smp->data.u.str.data);
1925 }
1926
1927 return ret;
1928}
1929
1930/* This produces a 32-bit hash of the concatenation of the first occurrence of
1931 * the Host header followed by the path component if it begins with a slash ('/').
1932 * This means that '*' will not be added, resulting in exactly the first Host
1933 * entry. If no Host header is found, then the path is used. The resulting value
1934 * is hashed using the url hash followed by a full avalanche hash and provides a
1935 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1936 * high-traffic sites without having to store whole paths.
1937 * this differs from the base32 functions in that it includes the url parameters
1938 * as well as the path
1939 */
1940static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1941{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001942 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001943 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001944 struct http_hdr_ctx ctx;
1945 struct htx_sl *sl;
1946 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001947 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001948
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001949 if (!htx)
1950 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001951
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001952 ctx.blk = NULL;
1953 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1954 /* OK we have the header value in ctx.value */
1955 while (ctx.value.len--)
1956 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001957 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001958
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001959 /* now retrieve the path */
1960 sl = http_get_stline(htx);
1961 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001962 if (path.len && *(path.ptr) == '/') {
1963 while (path.len--)
1964 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001965 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001966
Willy Tarreau79e57332018-10-02 16:01:16 +02001967 hash = full_hash(hash);
1968
1969 smp->data.type = SMP_T_SINT;
1970 smp->data.u.sint = hash;
1971 smp->flags = SMP_F_VOL_1ST;
1972 return 1;
1973}
1974
1975/* This concatenates the source address with the 32-bit hash of the Host and
1976 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1977 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1978 * on the source address length. The URL hash is stored before the address so
1979 * that in environments where IPv6 is insignificant, truncating the output to
1980 * 8 bytes would still work.
1981 */
1982static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1983{
1984 struct buffer *temp;
1985 struct connection *cli_conn = objt_conn(smp->sess->origin);
1986
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001987 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001988 return 0;
1989
1990 if (!smp_fetch_url32(args, smp, kw, private))
1991 return 0;
1992
1993 temp = get_trash_chunk();
1994 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1995 temp->data += sizeof(unsigned int);
1996
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001997 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001998 case AF_INET:
1999 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002000 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002001 4);
2002 temp->data += 4;
2003 break;
2004 case AF_INET6:
2005 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002006 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002007 16);
2008 temp->data += 16;
2009 break;
2010 default:
2011 return 0;
2012 }
2013
2014 smp->data.u.str = *temp;
2015 smp->data.type = SMP_T_BIN;
2016 return 1;
2017}
2018
2019/************************************************************************/
2020/* Other utility functions */
2021/************************************************************************/
2022
2023/* This function is used to validate the arguments passed to any "hdr" fetch
2024 * keyword. These keywords support an optional positive or negative occurrence
2025 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2026 * is assumed that the types are already the correct ones. Returns 0 on error,
2027 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2028 * error message in case of error, that the caller is responsible for freeing.
2029 * The initial location must either be freeable or NULL.
2030 * Note: this function's pointer is checked from Lua.
2031 */
2032int val_hdr(struct arg *arg, char **err_msg)
2033{
2034 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2035 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2036 return 0;
2037 }
2038 return 1;
2039}
2040
2041/************************************************************************/
2042/* All supported sample fetch keywords must be declared here. */
2043/************************************************************************/
2044
2045/* Note: must not be declared <const> as its list will be overwritten */
2046static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2047 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2048 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2049 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Yves Lafonb4d37082021-02-11 11:01:28 +01002050 { "baseq", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002051
2052 /* capture are allocated and are permanent in the stream */
2053 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2054
2055 /* retrieve these captures from the HTTP logs */
2056 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2057 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2058 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2059
2060 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2061 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2062
2063 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2064 * are only here to match the ACL's name, are request-only and are used
2065 * for ACL compatibility only.
2066 */
2067 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002068 { "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 +02002069 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2070 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2071
2072 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2073 * only here to match the ACL's name, are request-only and are used for
2074 * ACL compatibility only.
2075 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002076 { "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 +02002077 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2078 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2079 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2080
Christopher Fauleta4063562019-08-02 11:51:37 +02002081 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2082 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2083 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002084 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2085 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2086 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2087 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2088 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Faulete720c322020-09-02 17:25:18 +02002089 { "pathq", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002090 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2091
2092 /* HTTP protocol on the request path */
2093 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2094 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2095
2096 /* HTTP version on the request path */
2097 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2098 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2099
2100 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2101 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2102 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2103 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2104
2105 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2106 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2107
2108 /* HTTP version on the response path */
2109 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2110 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2111
Christopher Faulete596d182020-05-05 17:46:34 +02002112 { "res.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2113 { "res.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2114 { "res.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2115
2116 { "res.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2117 { "res.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2118
Willy Tarreau79e57332018-10-02 16:01:16 +02002119 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2120 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2121 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2122 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2123
2124 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2125 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2126 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2127 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2128 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2129 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2130 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2131
2132 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2133 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2134 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2135 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2136
2137 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2138 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2139 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2140 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2141 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2142 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2143 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2144
2145 /* scook is valid only on the response and is used for ACL compatibility */
2146 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2147 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2148 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2149 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2150
2151 /* shdr is valid only on the response and is used for ACL compatibility */
2152 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2153 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2154 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2155 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2156
2157 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2158 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2159 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2160 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2161 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2162 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2163 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2164 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2165 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2166 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Christopher Faulet16032ab2020-04-30 11:30:00 +02002167
Willy Tarreau79e57332018-10-02 16:01:16 +02002168 { /* END */ },
2169}};
2170
Willy Tarreau0108d902018-11-25 19:14:37 +01002171INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002172
2173/*
2174 * Local variables:
2175 * c-indent-level: 8
2176 * c-basic-offset: 8
2177 * End:
2178 */