blob: e5d8e48ceda87394892f166f4e495d6a924d1972 [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 Tarreau5edca2f2022-05-27 09:25:10 +020037#include <haproxy/sc_strm.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020038#include <haproxy/stream.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020039#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020040#include <haproxy/version.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020041
Willy Tarreau79e57332018-10-02 16:01:16 +020042
43/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Fauletef453ed2018-10-24 21:39:27 +020044static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
Richard Russo458eafb2019-07-31 11:45:56 -070045/* this is used to convert raw connection buffers to htx */
46static THREAD_LOCAL struct buffer static_raw_htx_chunk;
47static THREAD_LOCAL char *static_raw_htx_buf;
Christopher Fauletef453ed2018-10-24 21:39:27 +020048
Christopher Faulet89dc4992019-04-17 12:02:59 +020049#define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL)
50#define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL)
Willy Tarreau79e57332018-10-02 16:01:16 +020051
Richard Russo458eafb2019-07-31 11:45:56 -070052/* This function returns the static htx chunk, where raw connections get
53 * converted to HTX as needed for samplxsing.
54 */
55struct buffer *get_raw_htx_chunk(void)
56{
57 chunk_reset(&static_raw_htx_chunk);
58 return &static_raw_htx_chunk;
59}
60
61static int alloc_raw_htx_chunk_per_thread()
62{
63 static_raw_htx_buf = malloc(global.tune.bufsize);
64 if (!static_raw_htx_buf)
65 return 0;
66 chunk_init(&static_raw_htx_chunk, static_raw_htx_buf, global.tune.bufsize);
67 return 1;
68}
69
70static void free_raw_htx_chunk_per_thread()
71{
Willy Tarreau61cfdf42021-02-20 10:46:51 +010072 ha_free(&static_raw_htx_buf);
Richard Russo458eafb2019-07-31 11:45:56 -070073}
74
75REGISTER_PER_THREAD_ALLOC(alloc_raw_htx_chunk_per_thread);
76REGISTER_PER_THREAD_FREE(free_raw_htx_chunk_per_thread);
77
Willy Tarreau79e57332018-10-02 16:01:16 +020078/*
79 * Returns the data from Authorization header. Function may be called more
80 * than once so data is stored in txn->auth_data. When no header is found
81 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
82 * searching again for something we are unable to find anyway. However, if
83 * the result if valid, the cache is not reused because we would risk to
84 * have the credentials overwritten by another stream in parallel.
Willy Tarreaueae83722020-04-29 11:52:51 +020085 * The caller is responsible for passing a sample with a valid stream/txn,
86 * and a valid htx.
Willy Tarreau79e57332018-10-02 16:01:16 +020087 */
88
Christopher Fauletcd761952019-07-15 13:58:29 +020089static int get_http_auth(struct sample *smp, struct htx *htx)
Willy Tarreau79e57332018-10-02 16:01:16 +020090{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020091 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020092 struct http_txn *txn = s->txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020093 struct http_hdr_ctx ctx = { .blk = NULL };
94 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020095 struct buffer auth_method;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020096 char *p;
Willy Tarreau79e57332018-10-02 16:01:16 +020097 int len;
98
99#ifdef DEBUG_AUTH
100 printf("Auth for stream %p: %d\n", s, txn->auth.method);
101#endif
Willy Tarreau79e57332018-10-02 16:01:16 +0200102 if (txn->auth.method == HTTP_AUTH_WRONG)
103 return 0;
104
105 txn->auth.method = HTTP_AUTH_WRONG;
106
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200107 if (txn->flags & TX_USE_PX_CONN)
108 hdr = ist("Proxy-Authorization");
109 else
110 hdr = ist("Authorization");
Willy Tarreau79e57332018-10-02 16:01:16 +0200111
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200112 ctx.blk = NULL;
113 if (!http_find_header(htx, hdr, &ctx, 0))
114 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200115
Willy Tarreau17254932020-09-02 07:08:47 +0200116 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
117 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 +0200118 return 0;
Willy Tarreau17254932020-09-02 07:08:47 +0200119 len = p - ctx.value.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +0200120
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200121 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
122 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200123
Remi Tricot-Le Breton68c4eae2021-10-29 15:25:18 +0200124 /* According to RFC7235, there could be multiple spaces between the
125 * scheme and its value, we must skip all of them.
126 */
127 while (p < istend(ctx.value) && *p == ' ')
128 ++p;
129
130 chunk_initlen(&txn->auth.method_data, p, 0, istend(ctx.value) - p);
Willy Tarreau79e57332018-10-02 16:01:16 +0200131
132 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
133 struct buffer *http_auth = get_trash_chunk();
134
135 len = base64dec(txn->auth.method_data.area,
136 txn->auth.method_data.data,
137 http_auth->area, global.tune.bufsize - 1);
138
139 if (len < 0)
140 return 0;
141
142
143 http_auth->area[len] = '\0';
144
145 p = strchr(http_auth->area, ':');
146
147 if (!p)
148 return 0;
149
150 txn->auth.user = http_auth->area;
151 *p = '\0';
152 txn->auth.pass = p+1;
153
154 txn->auth.method = HTTP_AUTH_BASIC;
155 return 1;
Remi Tricot-Le Bretonf5dd3372021-10-01 15:36:53 +0200156 } else if (!strncasecmp("Bearer", auth_method.area, auth_method.data)) {
157 txn->auth.method = HTTP_AUTH_BEARER;
158 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200159 }
160
161 return 0;
162}
163
164/* This function ensures that the prerequisites for an L7 fetch are ready,
165 * which means that a request or response is ready. If some data is missing,
166 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200167 * to extract data from L7. If <vol> is non-null during a prefetch, another
168 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200169 *
170 * The function returns :
171 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
172 * decide whether or not an HTTP message is present ;
173 * NULL if the requested data cannot be fetched or if it is certain that
Willy Tarreaueae83722020-04-29 11:52:51 +0200174 * we'll never have any HTTP message there; this includes null strm or chn.
Willy Tarreaua6d98792020-08-12 14:04:52 +0200175 * NULL if the sample's direction does not match the channel's (i.e. the
176 * function was asked to work on the wrong channel)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200177 * The HTX message if ready
178 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200179struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, struct check *check, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200180{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200181 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200182 struct http_txn *txn = NULL;
183 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200184 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100185 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200186
Willy Tarreaua6d98792020-08-12 14:04:52 +0200187 if (chn &&
188 (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ && (chn->flags & CF_ISRESP)) ||
189 ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES && !(chn->flags & CF_ISRESP))))
190 return 0;
191
Christopher Fauletef453ed2018-10-24 21:39:27 +0200192 /* Note: it is possible that <s> is NULL when called before stream
193 * initialization (eg: tcp-request connection), so this function is the
194 * one responsible for guarding against this case for all HTTP users.
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200195 *
196 * In the health check context, the stream and the channel must be NULL
197 * and <check> must be set. In this case, only the input buffer,
198 * corresponding to the response, is considered. It is the caller
199 * responsibility to provide <check>.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200200 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200201 BUG_ON(check && (s || chn));
202 if (!s || !chn) {
203 if (check) {
204 htx = htxbuf(&check->bi);
205
206 /* Analyse not yet started */
207 if (htx_is_empty(htx) || htx->first == -1)
208 return NULL;
209
210 sl = http_get_stline(htx);
211 if (vol && !sl) {
212 /* The start-line was already forwarded, it is too late to fetch anything */
213 return NULL;
214 }
215 goto end;
216 }
217
Christopher Fauletef453ed2018-10-24 21:39:27 +0200218 return NULL;
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200219 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200220
Christopher Faulet75f619a2021-03-08 19:12:58 +0100221 if (!s->txn && !http_create_txn(s))
222 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200223 txn = s->txn;
224 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200225
Christopher Fauleteca88542019-04-03 10:12:42 +0200226 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200227 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200228
Christopher Faulet5aab0a32023-01-13 10:58:20 +0100229 if (htx->flags & HTX_FL_PARSING_ERROR)
Christopher Faulet89dc4992019-04-17 12:02:59 +0200230 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200231
Christopher Faulet89dc4992019-04-17 12:02:59 +0200232 if (msg->msg_state < HTTP_MSG_BODY) {
233 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200234 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200235 /* Parsing is done by the mux, just wait */
236 smp->flags |= SMP_F_MAY_CHANGE;
237 return NULL;
238 }
239 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200240 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200241 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200242 /* The start-line was already forwarded, it is too late to fetch anything */
243 return NULL;
244 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200245 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200246 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200247 struct buffer *buf;
248 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200249 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200250 union h1_sl h1sl;
251 unsigned int flags = HTX_FL_NONE;
252 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200253
Christopher Faulet89dc4992019-04-17 12:02:59 +0200254 /* no HTTP fetch on the response in TCP mode */
255 if (chn->flags & CF_ISRESP)
256 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200257
Christopher Faulet89dc4992019-04-17 12:02:59 +0200258 /* Now we are working on the request only */
259 buf = &chn->buf;
260 if (b_head(buf) + b_data(buf) > b_wrap(buf))
261 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200262
Christopher Faulet89dc4992019-04-17 12:02:59 +0200263 h1m_init_req(&h1m);
264 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
265 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
266 if (ret <= 0) {
267 /* Invalid or too big*/
268 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200269 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100270
Christopher Faulet89dc4992019-04-17 12:02:59 +0200271 /* wait for a full request */
272 smp->flags |= SMP_F_MAY_CHANGE;
273 return NULL;
274 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100275
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500276 /* OK we just got a valid HTTP message. We have to convert it
Christopher Faulet89dc4992019-04-17 12:02:59 +0200277 * into an HTX message.
278 */
279 if (unlikely(h1sl.rq.v.len == 0)) {
280 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
281 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200282 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200283 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200284 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200285
286 /* Set HTX start-line flags */
287 if (h1m.flags & H1_MF_VER_11)
288 flags |= HTX_SL_F_VER_11;
289 if (h1m.flags & H1_MF_XFER_ENC)
290 flags |= HTX_SL_F_XFER_ENC;
291 flags |= HTX_SL_F_XFER_LEN;
292 if (h1m.flags & H1_MF_CHNK)
293 flags |= HTX_SL_F_CHNK;
294 else if (h1m.flags & H1_MF_CLEN)
295 flags |= HTX_SL_F_CLEN;
296
Richard Russo458eafb2019-07-31 11:45:56 -0700297 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200298 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
299 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200300 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200301 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200302 }
303
304 /* OK we just got a valid HTTP message. If not already done by
305 * HTTP analyzers, we have some minor preparation to perform so
306 * that further checks can rely on HTTP tests.
307 */
308 if (sl && msg->msg_state < HTTP_MSG_BODY) {
309 if (!(chn->flags & CF_ISRESP)) {
310 txn->meth = sl->info.req.meth;
311 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
312 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200313 }
Christopher Faulet31850b42023-01-04 10:11:32 +0100314 else if (txn->status == -1)
Christopher Faulet89dc4992019-04-17 12:02:59 +0200315 txn->status = sl->info.res.status;
316 if (sl->flags & HTX_SL_F_VER_11)
317 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200318 }
319
320 /* everything's OK */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200321 end:
Christopher Fauletef453ed2018-10-24 21:39:27 +0200322 return htx;
323}
324
Willy Tarreau79e57332018-10-02 16:01:16 +0200325/* This function fetches the method of current HTTP request and stores
326 * it in the global pattern struct as a chunk. There are two possibilities :
327 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
328 * in <len> and <ptr> is NULL ;
329 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
330 * <len> to its length.
331 * This is intended to be used with pat_match_meth() only.
332 */
333static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
334{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200335 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200336 struct http_txn *txn;
Willy Tarreau2e2b79d2022-10-04 09:18:34 +0200337 struct htx *htx = NULL;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200338 int meth;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200339
Christopher Faulet12f6dbb2022-07-06 17:53:02 +0200340 txn = (smp->strm ? smp->strm->txn : NULL);
Willy Tarreaua6d98792020-08-12 14:04:52 +0200341 if (!txn)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200342 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200343
Willy Tarreaua88e8bf2022-07-10 13:13:52 +0200344 meth = txn->meth;
345 if (meth == HTTP_METH_OTHER) {
Christopher Fauletdbbdb252022-06-22 17:16:41 +0200346 htx = smp_prefetch_htx(smp, chn, NULL, 1);
347 if (!htx)
348 return 0;
Christopher Fauleteefcd8a2022-10-04 08:58:02 +0200349 meth = txn->meth;
Christopher Fauletdbbdb252022-06-22 17:16:41 +0200350 }
351
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200352 smp->data.type = SMP_T_METH;
353 smp->data.u.meth.meth = meth;
354 if (meth == HTTP_METH_OTHER) {
355 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200356
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200357 sl = http_get_stline(htx);
358 smp->flags |= SMP_F_CONST;
359 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
360 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200361 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200362 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200363 return 1;
364}
365
366static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
367{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200368 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200369 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200370 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200371 char *ptr;
372 int len;
373
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200374 if (!htx)
375 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200376
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200377 sl = http_get_stline(htx);
378 len = HTX_SL_REQ_VLEN(sl);
379 ptr = HTX_SL_REQ_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200380
381 while ((len-- > 0) && (*ptr++ != '/'));
382 if (len <= 0)
383 return 0;
384
385 smp->data.type = SMP_T_STR;
386 smp->data.u.str.area = ptr;
387 smp->data.u.str.data = len;
388
389 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
390 return 1;
391}
392
393static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
394{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200395 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200396 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200397 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200398 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200399 char *ptr;
400 int len;
401
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200402 if (!htx)
403 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200404
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200405 sl = http_get_stline(htx);
406 len = HTX_SL_RES_VLEN(sl);
407 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200408
409 while ((len-- > 0) && (*ptr++ != '/'));
410 if (len <= 0)
411 return 0;
412
413 smp->data.type = SMP_T_STR;
414 smp->data.u.str.area = ptr;
415 smp->data.u.str.data = len;
416
417 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
418 return 1;
419}
420
421/* 3. Check on Status Code. We manipulate integers here. */
422static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
423{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200424 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200425 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200426 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200427 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200428 char *ptr;
429 int len;
430
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200431 if (!htx)
432 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200433
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200434 sl = http_get_stline(htx);
435 len = HTX_SL_RES_CLEN(sl);
436 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200437
438 smp->data.type = SMP_T_SINT;
439 smp->data.u.sint = __strl2ui(ptr, len);
440 smp->flags = SMP_F_VOL_1ST;
441 return 1;
442}
443
444static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
445{
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100446 struct ist unique_id;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100447
Willy Tarreau79e57332018-10-02 16:01:16 +0200448 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
449 return 0;
450
Willy Tarreaua1062a42020-04-29 11:50:38 +0200451 if (!smp->strm)
452 return 0;
453
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100454 unique_id = stream_generate_unique_id(smp->strm, &smp->sess->fe->format_unique_id);
455 if (!isttest(unique_id))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100456 return 0;
457
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100458 smp->data.u.str.area = smp->strm->unique_id.ptr;
459 smp->data.u.str.data = smp->strm->unique_id.len;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100460 smp->data.type = SMP_T_STR;
Willy Tarreau79e57332018-10-02 16:01:16 +0200461 smp->flags = SMP_F_CONST;
462 return 1;
463}
464
465/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800466 * empty line which separes headers from the body. This is useful
467 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200468 */
469static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
470{
Christopher Faulete596d182020-05-05 17:46:34 +0200471 /* possible keywords: req.hdrs, res.hdrs */
472 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200473 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200474 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200475 struct buffer *temp;
476 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200477
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200478 if (!htx)
479 return 0;
480 temp = get_trash_chunk();
481 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
482 struct htx_blk *blk = htx_get_blk(htx, pos);
483 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200484
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200485 if (type == HTX_BLK_HDR) {
486 struct ist n = htx_get_blk_name(htx, blk);
487 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200488
Christopher Faulet53a899b2019-10-08 16:38:42 +0200489 if (!h1_format_htx_hdr(n, v, temp))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200490 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200491 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200492 else if (type == HTX_BLK_EOH) {
493 if (!chunk_memcat(temp, "\r\n", 2))
494 return 0;
495 break;
496 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200497 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200498 smp->data.type = SMP_T_STR;
499 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200500 return 1;
501}
502
503/* Returns the header request in a length/value encoded format.
504 * This is useful for exchanges with the SPOE.
505 *
506 * A "length value" is a multibyte code encoding numbers. It uses the
507 * SPOE format. The encoding is the following:
508 *
509 * Each couple "header name" / "header value" is composed
510 * like this:
511 * "length value" "header name bytes"
512 * "length value" "header value bytes"
513 * When the last header is reached, the header name and the header
514 * value are empty. Their length are 0
515 */
516static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
517{
Christopher Faulete596d182020-05-05 17:46:34 +0200518 /* possible keywords: req.hdrs_bin, res.hdrs_bin */
519 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200520 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200521 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200522 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200523 char *p, *end;
524 int32_t pos;
525 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200526
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200527 if (!htx)
528 return 0;
529 temp = get_trash_chunk();
530 p = temp->area;
531 end = temp->area + temp->size;
532 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
533 struct htx_blk *blk = htx_get_blk(htx, pos);
534 enum htx_blk_type type = htx_get_blk_type(blk);
535 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200536
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200537 if (type == HTX_BLK_HDR) {
538 n = htx_get_blk_name(htx,blk);
539 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200540
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200541 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200542 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200543 if (ret == -1)
544 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200545 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200546 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200547 memcpy(p, n.ptr, n.len);
548 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200549
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200550 /* encode the header value. */
551 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200552 if (ret == -1)
553 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200554 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200555 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200556 memcpy(p, v.ptr, v.len);
557 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200558
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200559 }
560 else if (type == HTX_BLK_EOH) {
561 /* encode the end of the header list with empty
562 * header name and header value.
563 */
564 ret = encode_varint(0, &p, end);
565 if (ret == -1)
566 return 0;
567 ret = encode_varint(0, &p, end);
568 if (ret == -1)
569 return 0;
570 break;
571 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200572 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200573
574 /* Initialise sample data which will be filled. */
575 smp->data.type = SMP_T_BIN;
576 smp->data.u.str.area = temp->area;
577 smp->data.u.str.data = p - temp->area;
578 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200579 return 1;
580}
581
582/* returns the longest available part of the body. This requires that the body
583 * has been waited for using http-buffer-request.
584 */
585static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
586{
Christopher Faulete596d182020-05-05 17:46:34 +0200587 /* possible keywords: req.body, res.body */
588 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200589 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200590 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200591 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200592 int32_t pos;
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100593 int finished = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200594
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200595 if (!htx)
596 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200597
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200598 temp = get_trash_chunk();
599 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
600 struct htx_blk *blk = htx_get_blk(htx, pos);
601 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200602
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100603 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) {
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100604 finished = 1;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200605 break;
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100606 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200607 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +0200608 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200609 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200610 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200611 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200612
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200613 smp->data.type = SMP_T_BIN;
614 smp->data.u.str = *temp;
615 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200616
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100617 if (!finished && (check || (chn && !channel_full(chn, global.tune.maxrewrite) &&
Christopher Fauletca5309a2023-04-17 16:17:32 +0200618 !(chn_prod(chn)->flags & (SC_FL_EOI|SC_FL_EOS|SC_FL_ABRT_DONE)))))
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200619 smp->flags |= SMP_F_MAY_CHANGE;
620
Willy Tarreau79e57332018-10-02 16:01:16 +0200621 return 1;
622}
623
624
625/* returns the available length of the body. This requires that the body
626 * has been waited for using http-buffer-request.
627 */
628static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
629{
Christopher Faulete596d182020-05-05 17:46:34 +0200630 /* possible keywords: req.body_len, res.body_len */
631 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200632 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200633 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200634 int32_t pos;
635 unsigned long long len = 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100636
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200637 if (!htx)
638 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100639
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200640 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
641 struct htx_blk *blk = htx_get_blk(htx, pos);
642 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100643
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100644 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200645 break;
646 if (type == HTX_BLK_DATA)
647 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200648 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200649
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200650 smp->data.type = SMP_T_SINT;
651 smp->data.u.sint = len;
652 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200653 return 1;
654}
655
656
657/* returns the advertised length of the body, or the advertised size of the
658 * chunks available in the buffer. This requires that the body has been waited
659 * for using http-buffer-request.
660 */
661static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
662{
Christopher Faulete596d182020-05-05 17:46:34 +0200663 /* possible keywords: req.body_size, res.body_size */
664 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200665 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200666 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200667 int32_t pos;
668 unsigned long long len = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200669
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200670 if (!htx)
671 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100672
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200673 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
674 struct htx_blk *blk = htx_get_blk(htx, pos);
675 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100676
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100677 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200678 break;
679 if (type == HTX_BLK_DATA)
680 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200681 }
Christopher Faulet2e47e3a2023-01-13 11:40:24 +0100682 if (htx->extra != HTX_UNKOWN_PAYLOAD_LENGTH)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200683 len += htx->extra;
Willy Tarreau79e57332018-10-02 16:01:16 +0200684
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200685 smp->data.type = SMP_T_SINT;
686 smp->data.u.sint = len;
687 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200688 return 1;
689}
690
691
692/* 4. Check on URL/URI. A pointer to the URI is stored. */
693static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
694{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200695 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200696 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200697 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200698
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200699 if (!htx)
700 return 0;
701 sl = http_get_stline(htx);
702 smp->data.type = SMP_T_STR;
703 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
704 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
705 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200706 return 1;
707}
708
709static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
710{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200711 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200712 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200713 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200714 struct sockaddr_storage addr;
715
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200716 memset(&addr, 0, sizeof(addr));
717
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200718 if (!htx)
719 return 0;
720 sl = http_get_stline(htx);
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200721 if (url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL) < 0)
722 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200723
Willy Tarreau48584642021-05-09 10:32:54 +0200724 if (addr.ss_family != AF_INET)
Willy Tarreau79e57332018-10-02 16:01:16 +0200725 return 0;
726
727 smp->data.type = SMP_T_IPV4;
728 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
729 smp->flags = 0;
730 return 1;
731}
732
733static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
734{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200735 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200736 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200737 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200738 struct sockaddr_storage addr;
739
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200740 memset(&addr, 0, sizeof(addr));
741
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200742 if (!htx)
743 return 0;
744 sl = http_get_stline(htx);
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200745 if (url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL) < 0)
746 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200747
Willy Tarreau48584642021-05-09 10:32:54 +0200748 if (addr.ss_family != AF_INET)
Willy Tarreau79e57332018-10-02 16:01:16 +0200749 return 0;
750
751 smp->data.type = SMP_T_SINT;
Willy Tarreau48584642021-05-09 10:32:54 +0200752 smp->data.u.sint = get_host_port(&addr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200753 smp->flags = 0;
754 return 1;
755}
756
757/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
758 * Accepts an optional argument of type string containing the header field name,
759 * and an optional argument of type signed or unsigned integer to request an
760 * explicit occurrence of the header. Note that in the event of a missing name,
761 * headers are considered from the first one. It does not stop on commas and
762 * returns full lines instead (useful for User-Agent or Date for example).
763 */
764static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
765{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200766 /* possible keywords: req.fhdr, res.fhdr */
767 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200768 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200769 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200770 struct http_hdr_ctx *ctx = smp->ctx.a[0];
771 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200772 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200773
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200774 if (!ctx) {
775 /* first call */
776 ctx = &static_http_hdr_ctx;
777 ctx->blk = NULL;
778 smp->ctx.a[0] = ctx;
779 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200780
Christopher Faulet623af932021-01-29 11:22:15 +0100781 if (args[0].type != ARGT_STR)
782 return 0;
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100783 name = ist2(args[0].data.str.area, args[0].data.str.data);
Willy Tarreau79e57332018-10-02 16:01:16 +0200784
Christopher Faulet623af932021-01-29 11:22:15 +0100785 if (args[1].type == ARGT_SINT)
786 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200787
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200788 if (!htx)
789 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200790
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200791 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
792 /* search for header from the beginning */
793 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200794
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200795 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
796 /* no explicit occurrence and single fetch => last header by default */
797 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200798
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200799 if (!occ)
800 /* prepare to report multiple occurrences for ACL fetches */
801 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200802
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200803 smp->data.type = SMP_T_STR;
804 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
805 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
806 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200807 smp->flags &= ~SMP_F_NOT_LAST;
808 return 0;
809}
810
811/* 6. Check on HTTP header count. The number of occurrences is returned.
812 * Accepts exactly 1 argument of type string. It does not stop on commas and
813 * returns full lines instead (useful for User-Agent or Date for example).
814 */
815static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
816{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200817 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
818 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200819 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200820 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200821 struct http_hdr_ctx ctx;
822 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200823 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200824
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200825 if (!htx)
826 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200827
Christopher Faulet623af932021-01-29 11:22:15 +0100828 if (args->type == ARGT_STR) {
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100829 name = ist2(args->data.str.area, args->data.str.data);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200830 } else {
Tim Duesterhus68a088d2021-02-28 16:11:37 +0100831 name = IST_NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200832 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200833
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200834 ctx.blk = NULL;
835 cnt = 0;
836 while (http_find_header(htx, name, &ctx, 1))
837 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200838 smp->data.type = SMP_T_SINT;
839 smp->data.u.sint = cnt;
840 smp->flags = SMP_F_VOL_HDR;
841 return 1;
842}
843
844static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
845{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200846 /* possible keywords: req.hdr_names, res.hdr_names */
847 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200848 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200849 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200850 struct buffer *temp;
851 char del = ',';
852
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200853 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200854
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200855 if (!htx)
856 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200857
Christopher Faulet623af932021-01-29 11:22:15 +0100858 if (args->type == ARGT_STR)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200859 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200860
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200861 temp = get_trash_chunk();
862 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
863 struct htx_blk *blk = htx_get_blk(htx, pos);
864 enum htx_blk_type type = htx_get_blk_type(blk);
865 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200866
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200867 if (type == HTX_BLK_EOH)
868 break;
869 if (type != HTX_BLK_HDR)
870 continue;
871 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200872
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200873 if (temp->data)
874 temp->area[temp->data++] = del;
Tim Duesterhus9f7ed8a2021-11-08 09:05:04 +0100875 chunk_istcat(temp, n);
Willy Tarreau79e57332018-10-02 16:01:16 +0200876 }
877
878 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200879 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200880 smp->flags = SMP_F_VOL_HDR;
881 return 1;
882}
883
884/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
885 * Accepts an optional argument of type string containing the header field name,
886 * and an optional argument of type signed or unsigned integer to request an
887 * explicit occurrence of the header. Note that in the event of a missing name,
888 * headers are considered from the first one.
889 */
890static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
891{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200892 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
893 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200894 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200895 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200896 struct http_hdr_ctx *ctx = smp->ctx.a[0];
897 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200898 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200899
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200900 if (!ctx) {
901 /* first call */
902 ctx = &static_http_hdr_ctx;
903 ctx->blk = NULL;
904 smp->ctx.a[0] = ctx;
905 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200906
Christopher Faulet623af932021-01-29 11:22:15 +0100907 if (args[0].type != ARGT_STR)
908 return 0;
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100909 name = ist2(args[0].data.str.area, args[0].data.str.data);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200910
Christopher Faulet623af932021-01-29 11:22:15 +0100911 if (args[1].type == ARGT_SINT)
912 occ = args[1].data.sint;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200913
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200914 if (!htx)
915 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200916
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200917 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
918 /* search for header from the beginning */
919 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200920
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200921 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
922 /* no explicit occurrence and single fetch => last header by default */
923 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200924
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200925 if (!occ)
926 /* prepare to report multiple occurrences for ACL fetches */
927 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200928
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200929 smp->data.type = SMP_T_STR;
930 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
931 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
932 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200933
934 smp->flags &= ~SMP_F_NOT_LAST;
935 return 0;
936}
937
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200938/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
939 * the right channel. So instead of duplicating the code, we just change the
940 * keyword and then fallback on smp_fetch_hdr().
941 */
942static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
943{
944 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
945 return smp_fetch_hdr(args, smp, kw, private);
946}
947
Willy Tarreau79e57332018-10-02 16:01:16 +0200948/* 6. Check on HTTP header count. The number of occurrences is returned.
949 * Accepts exactly 1 argument of type string.
950 */
951static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
952{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200953 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
954 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200955 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200956 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200957 struct http_hdr_ctx ctx;
958 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200959 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200960
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200961 if (!htx)
962 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200963
Christopher Faulet623af932021-01-29 11:22:15 +0100964 if (args->type == ARGT_STR) {
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100965 name = ist2(args->data.str.area, args->data.str.data);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200966 } else {
Tim Duesterhus68a088d2021-02-28 16:11:37 +0100967 name = IST_NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200968 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200969
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200970 ctx.blk = NULL;
971 cnt = 0;
972 while (http_find_header(htx, name, &ctx, 0))
973 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200974
975 smp->data.type = SMP_T_SINT;
976 smp->data.u.sint = cnt;
977 smp->flags = SMP_F_VOL_HDR;
978 return 1;
979}
980
981/* Fetch an HTTP header's integer value. The integer value is returned. It
982 * takes a mandatory argument of type string and an optional one of type int
983 * to designate a specific occurrence. It returns an unsigned integer, which
984 * may or may not be appropriate for everything.
985 */
986static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
987{
988 int ret = smp_fetch_hdr(args, smp, kw, private);
989
990 if (ret > 0) {
991 smp->data.type = SMP_T_SINT;
992 smp->data.u.sint = strl2ic(smp->data.u.str.area,
993 smp->data.u.str.data);
994 }
995
996 return ret;
997}
998
999/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
1000 * and an optional one of type int to designate a specific occurrence.
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001001 * It returns an IPv4 or IPv6 address. Addresses surrounded by invalid chars
1002 * are rejected. However IPv4 addresses may be followed with a colon and a
1003 * valid port number.
Willy Tarreau79e57332018-10-02 16:01:16 +02001004 */
1005static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1006{
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001007 struct buffer *temp = get_trash_chunk();
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001008 int ret, len;
1009 int port;
Willy Tarreau79e57332018-10-02 16:01:16 +02001010
1011 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001012 if (smp->data.u.str.data < temp->size - 1) {
1013 memcpy(temp->area, smp->data.u.str.area,
1014 smp->data.u.str.data);
1015 temp->area[smp->data.u.str.data] = '\0';
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001016 len = url2ipv4((char *) temp->area, &smp->data.u.ipv4);
Willy Tarreau645dc082021-03-31 11:41:36 +02001017 if (len > 0 && len == smp->data.u.str.data) {
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001018 /* plain IPv4 address */
1019 smp->data.type = SMP_T_IPV4;
1020 break;
1021 } else if (len > 0 && temp->area[len] == ':' &&
1022 strl2irc(temp->area + len + 1, smp->data.u.str.data - len - 1, &port) == 0 &&
1023 port >= 0 && port <= 65535) {
1024 /* IPv4 address suffixed with ':' followed by a valid port number */
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001025 smp->data.type = SMP_T_IPV4;
1026 break;
Oto Valekd1773e62023-02-24 21:39:56 +01001027 } else if (temp->area[0] == '[' && temp->area[smp->data.u.str.data-1] == ']') {
1028 /* IPv6 address enclosed in square brackets */
1029 temp->area[smp->data.u.str.data-1] = '\0';
1030 if (inet_pton(AF_INET6, temp->area+1, &smp->data.u.ipv6)) {
1031 smp->data.type = SMP_T_IPV6;
1032 break;
1033 }
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001034 } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
Oto Valekd1773e62023-02-24 21:39:56 +01001035 /* plain IPv6 address */
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001036 smp->data.type = SMP_T_IPV6;
1037 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001038 }
1039 }
1040
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001041 /* if the header doesn't match an IP address, fetch next one */
1042 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001043 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001044 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001045 return ret;
1046}
Willy Tarreau79e57332018-10-02 16:01:16 +02001047
Christopher Faulete720c322020-09-02 17:25:18 +02001048/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at the
1049 * first '/' after the possible hostname. It ends before the possible '?' except
1050 * for 'pathq' keyword.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001051 */
1052static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1053{
1054 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001055 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001056 struct htx_sl *sl;
1057 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02001058 struct http_uri_parser parser;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001059
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001060 if (!htx)
1061 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001062
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001063 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02001064 parser = http_uri_parser_init(htx_sl_req_uri(sl));
Christopher Faulete720c322020-09-02 17:25:18 +02001065
Yves Lafonb4d37082021-02-11 11:01:28 +01001066 if (kw[4] == 'q' && (kw[0] == 'p' || kw[0] == 'b')) // pathq or baseq
Amaury Denoyellec453f952021-07-06 11:40:12 +02001067 path = http_parse_path(&parser);
Christopher Faulete720c322020-09-02 17:25:18 +02001068 else
Amaury Denoyellec453f952021-07-06 11:40:12 +02001069 path = iststop(http_parse_path(&parser), '?');
Christopher Faulete720c322020-09-02 17:25:18 +02001070
Tim Duesterhused526372020-03-05 17:56:33 +01001071 if (!isttest(path))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001072 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001073
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001074 /* OK, we got the '/' ! */
1075 smp->data.type = SMP_T_STR;
1076 smp->data.u.str.area = path.ptr;
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001077 smp->data.u.str.data = path.len;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001078 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001079 return 1;
1080}
1081
1082/* This produces a concatenation of the first occurrence of the Host header
1083 * followed by the path component if it begins with a slash ('/'). This means
1084 * that '*' will not be added, resulting in exactly the first Host entry.
1085 * If no Host header is found, then the path is returned as-is. The returned
1086 * value is stored in the trash so it does not need to be marked constant.
1087 * The returned sample is of type string.
1088 */
1089static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1090{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001091 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001092 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001093 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001094 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001095 struct http_hdr_ctx ctx;
1096 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02001097 struct http_uri_parser parser;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001098
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001099 if (!htx)
1100 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001101
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001102 ctx.blk = NULL;
1103 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1104 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001105
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001106 /* OK we have the header value in ctx.value */
1107 temp = get_trash_chunk();
Tim Duesterhus77508502022-03-15 13:11:06 +01001108 chunk_istcat(temp, ctx.value);
Willy Tarreau79e57332018-10-02 16:01:16 +02001109
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001110 /* now retrieve the path */
1111 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02001112 parser = http_uri_parser_init(htx_sl_req_uri(sl));
1113 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01001114 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001115 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001116
Yves Lafonb4d37082021-02-11 11:01:28 +01001117 if (kw[4] == 'q' && kw[0] == 'b') { // baseq
1118 len = path.len;
1119 } else {
1120 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1121 ;
1122 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001123
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001124 if (len && *(path.ptr) == '/')
1125 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001126 }
1127
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001128 smp->data.type = SMP_T_STR;
1129 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001130 smp->flags = SMP_F_VOL_1ST;
1131 return 1;
1132}
1133
1134/* This produces a 32-bit hash of the concatenation of the first occurrence of
1135 * the Host header followed by the path component if it begins with a slash ('/').
1136 * This means that '*' will not be added, resulting in exactly the first Host
1137 * entry. If no Host header is found, then the path is used. The resulting value
1138 * is hashed using the path hash followed by a full avalanche hash and provides a
1139 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1140 * high-traffic sites without having to store whole paths.
1141 */
1142static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1143{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001144 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001145 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001146 struct htx_sl *sl;
1147 struct http_hdr_ctx ctx;
1148 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001149 unsigned int hash = 0;
Amaury Denoyellec453f952021-07-06 11:40:12 +02001150 struct http_uri_parser parser;
Willy Tarreau79e57332018-10-02 16:01:16 +02001151
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001152 if (!htx)
1153 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001154
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001155 ctx.blk = NULL;
1156 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1157 /* OK we have the header value in ctx.value */
1158 while (ctx.value.len--)
1159 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001160 }
1161
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001162 /* now retrieve the path */
1163 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02001164 parser = http_uri_parser_init(htx_sl_req_uri(sl));
1165 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01001166 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001167 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001168
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001169 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1170 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001171
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001172 if (len && *(path.ptr) == '/') {
1173 while (len--)
1174 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001175 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001176 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001177
Willy Tarreau79e57332018-10-02 16:01:16 +02001178 hash = full_hash(hash);
1179
1180 smp->data.type = SMP_T_SINT;
1181 smp->data.u.sint = hash;
1182 smp->flags = SMP_F_VOL_1ST;
1183 return 1;
1184}
1185
1186/* This concatenates the source address with the 32-bit hash of the Host and
1187 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1188 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1189 * on the source address length. The path hash is stored before the address so
1190 * that in environments where IPv6 is insignificant, truncating the output to
1191 * 8 bytes would still work.
1192 */
1193static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1194{
Willy Tarreaud68ff012022-05-27 08:57:21 +02001195 const struct sockaddr_storage *src = (smp->strm ? sc_src(smp->strm->scf) : NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +02001196 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001197
Christopher Faulet6fc817a2021-10-25 07:48:27 +02001198 if (!src)
Willy Tarreau79e57332018-10-02 16:01:16 +02001199 return 0;
1200
1201 if (!smp_fetch_base32(args, smp, kw, private))
1202 return 0;
1203
1204 temp = get_trash_chunk();
1205 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1206 temp->data += sizeof(unsigned int);
1207
Christopher Faulet6fc817a2021-10-25 07:48:27 +02001208 switch (src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001209 case AF_INET:
1210 memcpy(temp->area + temp->data,
Christopher Faulet6fc817a2021-10-25 07:48:27 +02001211 &((struct sockaddr_in *)src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001212 4);
1213 temp->data += 4;
1214 break;
1215 case AF_INET6:
1216 memcpy(temp->area + temp->data,
Christopher Faulet6fc817a2021-10-25 07:48:27 +02001217 &((struct sockaddr_in6 *)src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001218 16);
1219 temp->data += 16;
1220 break;
1221 default:
1222 return 0;
1223 }
1224
1225 smp->data.u.str = *temp;
1226 smp->data.type = SMP_T_BIN;
1227 return 1;
1228}
1229
1230/* Extracts the query string, which comes after the question mark '?'. If no
1231 * question mark is found, nothing is returned. Otherwise it returns a sample
1232 * of type string carrying the whole query string.
1233 */
1234static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1235{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001236 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001237 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001238 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001239 char *ptr, *end;
1240
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001241 if (!htx)
1242 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001243
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001244 sl = http_get_stline(htx);
1245 ptr = HTX_SL_REQ_UPTR(sl);
1246 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001247
1248 /* look up the '?' */
1249 do {
1250 if (ptr == end)
1251 return 0;
1252 } while (*ptr++ != '?');
1253
1254 smp->data.type = SMP_T_STR;
1255 smp->data.u.str.area = ptr;
1256 smp->data.u.str.data = end - ptr;
1257 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1258 return 1;
1259}
1260
1261static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1262{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001263 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001264 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001265
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001266 if (!htx)
1267 return 0;
1268 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001269 smp->data.u.sint = 1;
1270 return 1;
1271}
1272
1273/* return a valid test if the current request is the first one on the connection */
1274static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1275{
Willy Tarreau79512b62020-04-29 11:52:13 +02001276 if (!smp->strm)
1277 return 0;
1278
Willy Tarreau79e57332018-10-02 16:01:16 +02001279 smp->data.type = SMP_T_BOOL;
1280 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1281 return 1;
1282}
1283
Christopher Fauleta4063562019-08-02 11:51:37 +02001284/* Fetch the authentication method if there is an Authorization header. It
1285 * relies on get_http_auth()
1286 */
1287static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1288{
1289 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001290 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001291 struct http_txn *txn;
1292
1293 if (!htx)
1294 return 0;
1295
1296 txn = smp->strm->txn;
1297 if (!get_http_auth(smp, htx))
1298 return 0;
1299
1300 switch (txn->auth.method) {
1301 case HTTP_AUTH_BASIC:
1302 smp->data.u.str.area = "Basic";
1303 smp->data.u.str.data = 5;
1304 break;
1305 case HTTP_AUTH_DIGEST:
1306 /* Unexpected because not supported */
1307 smp->data.u.str.area = "Digest";
1308 smp->data.u.str.data = 6;
1309 break;
Remi Tricot-Le Bretonf5dd3372021-10-01 15:36:53 +02001310 case HTTP_AUTH_BEARER:
1311 smp->data.u.str.area = "Bearer";
1312 smp->data.u.str.data = 6;
1313 break;
Christopher Fauleta4063562019-08-02 11:51:37 +02001314 default:
1315 return 0;
1316 }
1317
1318 smp->data.type = SMP_T_STR;
1319 smp->flags = SMP_F_CONST;
1320 return 1;
1321}
1322
1323/* Fetch the user supplied if there is an Authorization header. It relies on
1324 * get_http_auth()
1325 */
1326static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1327{
1328 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001329 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001330 struct http_txn *txn;
1331
1332 if (!htx)
1333 return 0;
1334
1335 txn = smp->strm->txn;
Remi Tricot-Le Bretonf5dd3372021-10-01 15:36:53 +02001336 if (!get_http_auth(smp, htx) || txn->auth.method != HTTP_AUTH_BASIC)
Christopher Fauleta4063562019-08-02 11:51:37 +02001337 return 0;
1338
1339 smp->data.type = SMP_T_STR;
1340 smp->data.u.str.area = txn->auth.user;
1341 smp->data.u.str.data = strlen(txn->auth.user);
1342 smp->flags = SMP_F_CONST;
1343 return 1;
1344}
1345
1346/* Fetch the password supplied if there is an Authorization header. It relies on
1347 * get_http_auth()
1348 */
1349static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1350{
1351 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001352 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001353 struct http_txn *txn;
1354
1355 if (!htx)
1356 return 0;
1357
1358 txn = smp->strm->txn;
Remi Tricot-Le Bretonf5dd3372021-10-01 15:36:53 +02001359 if (!get_http_auth(smp, htx) || txn->auth.method != HTTP_AUTH_BASIC)
Christopher Fauleta4063562019-08-02 11:51:37 +02001360 return 0;
1361
1362 smp->data.type = SMP_T_STR;
1363 smp->data.u.str.area = txn->auth.pass;
1364 smp->data.u.str.data = strlen(txn->auth.pass);
1365 smp->flags = SMP_F_CONST;
1366 return 1;
1367}
1368
Remi Tricot-Le Bretonf5dd3372021-10-01 15:36:53 +02001369static int smp_fetch_http_auth_bearer(const struct arg *args, struct sample *smp, const char *kw, void *private)
1370{
1371 struct channel *chn = SMP_REQ_CHN(smp);
1372 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
1373 struct http_txn *txn;
1374 struct buffer bearer_val = {};
1375
1376 if (!htx)
1377 return 0;
1378
1379 if (args->type == ARGT_STR) {
1380 struct http_hdr_ctx ctx;
1381 struct ist hdr_name = ist2(args->data.str.area, args->data.str.data);
1382
1383 ctx.blk = NULL;
1384 if (http_find_header(htx, hdr_name, &ctx, 0)) {
Remi Tricot-Le Breton7da35bf2021-10-29 15:25:19 +02001385 struct ist type = istsplit(&ctx.value, ' ');
1386
1387 /* There must be "at least" one space character between
1388 * the scheme and the following value so ctx.value might
1389 * still have leading spaces here (see RFC7235).
1390 */
1391 ctx.value = istskip(ctx.value, ' ');
1392
1393 if (isteqi(type, ist("Bearer")) && istlen(ctx.value))
1394 chunk_initlen(&bearer_val, istptr(ctx.value), 0, istlen(ctx.value));
Remi Tricot-Le Bretonf5dd3372021-10-01 15:36:53 +02001395 }
1396 }
1397 else {
1398 txn = smp->strm->txn;
1399 if (!get_http_auth(smp, htx) || txn->auth.method != HTTP_AUTH_BEARER)
1400 return 0;
1401
1402 bearer_val = txn->auth.method_data;
1403 }
1404
1405 smp->data.type = SMP_T_STR;
1406 smp->data.u.str = bearer_val;
1407 smp->flags = SMP_F_CONST;
1408 return 1;
1409}
1410
Willy Tarreau79e57332018-10-02 16:01:16 +02001411/* Accepts exactly 1 argument of type userlist */
1412static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1413{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001414 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001415 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001416
Christopher Faulet623af932021-01-29 11:22:15 +01001417 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001418 return 0;
1419
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001420 if (!htx)
1421 return 0;
Remi Tricot-Le Bretonf5dd3372021-10-01 15:36:53 +02001422 if (!get_http_auth(smp, htx) || smp->strm->txn->auth.method != HTTP_AUTH_BASIC)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001423 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001424
1425 smp->data.type = SMP_T_BOOL;
1426 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001427 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001428 return 1;
1429}
1430
1431/* Accepts exactly 1 argument of type userlist */
1432static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1433{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001434 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001435 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001436
Christopher Faulet623af932021-01-29 11:22:15 +01001437 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001438 return 0;
1439
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001440 if (!htx)
1441 return 0;
Remi Tricot-Le Bretonf5dd3372021-10-01 15:36:53 +02001442 if (!get_http_auth(smp, htx) || smp->strm->txn->auth.method != HTTP_AUTH_BASIC)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001443 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001444
Willy Tarreau79e57332018-10-02 16:01:16 +02001445 /* if the user does not belong to the userlist or has a wrong password,
1446 * report that it unconditionally does not match. Otherwise we return
1447 * a string containing the username.
1448 */
1449 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1450 smp->strm->txn->auth.pass))
1451 return 0;
1452
1453 /* pat_match_auth() will need the user list */
1454 smp->ctx.a[0] = args->data.usr;
1455
1456 smp->data.type = SMP_T_STR;
1457 smp->flags = SMP_F_CONST;
1458 smp->data.u.str.area = smp->strm->txn->auth.user;
1459 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1460
1461 return 1;
1462}
1463
1464/* Fetch a captured HTTP request header. The index is the position of
1465 * the "capture" option in the configuration file
1466 */
1467static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1468{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001469 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001470 int idx;
1471
Christopher Faulet623af932021-01-29 11:22:15 +01001472 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001473 return 0;
1474
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001475 if (!smp->strm)
1476 return 0;
1477
1478 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001479 idx = args->data.sint;
1480
1481 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1482 return 0;
1483
1484 smp->data.type = SMP_T_STR;
1485 smp->flags |= SMP_F_CONST;
1486 smp->data.u.str.area = smp->strm->req_cap[idx];
1487 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1488
1489 return 1;
1490}
1491
1492/* Fetch a captured HTTP response header. The index is the position of
1493 * the "capture" option in the configuration file
1494 */
1495static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1496{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001497 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001498 int idx;
1499
Christopher Faulet623af932021-01-29 11:22:15 +01001500 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001501 return 0;
1502
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001503 if (!smp->strm)
1504 return 0;
1505
1506 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001507 idx = args->data.sint;
1508
1509 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1510 return 0;
1511
1512 smp->data.type = SMP_T_STR;
1513 smp->flags |= SMP_F_CONST;
1514 smp->data.u.str.area = smp->strm->res_cap[idx];
1515 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1516
1517 return 1;
1518}
1519
1520/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1521static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1522{
1523 struct buffer *temp;
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001524 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001525 char *ptr;
1526
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001527 if (!smp->strm)
1528 return 0;
1529
1530 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001531 if (!txn || !txn->uri)
1532 return 0;
1533
1534 ptr = txn->uri;
1535
1536 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1537 ptr++;
1538
1539 temp = get_trash_chunk();
1540 temp->area = txn->uri;
1541 temp->data = ptr - txn->uri;
1542 smp->data.u.str = *temp;
1543 smp->data.type = SMP_T_STR;
1544 smp->flags = SMP_F_CONST;
1545
1546 return 1;
1547
1548}
1549
1550/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1551static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1552{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001553 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001554 struct ist path;
1555 const char *ptr;
Amaury Denoyellec453f952021-07-06 11:40:12 +02001556 struct http_uri_parser parser;
Willy Tarreau79e57332018-10-02 16:01:16 +02001557
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001558 if (!smp->strm)
1559 return 0;
1560
1561 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001562 if (!txn || !txn->uri)
1563 return 0;
1564
1565 ptr = txn->uri;
1566
1567 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1568 ptr++;
1569
1570 if (!*ptr)
1571 return 0;
1572
Christopher Faulet78337bb2018-11-15 14:35:18 +01001573 /* skip the first space and find space after URI */
1574 path = ist2(++ptr, 0);
1575 while (*ptr != ' ' && *ptr != '\0')
1576 ptr++;
1577 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001578
Amaury Denoyellec453f952021-07-06 11:40:12 +02001579 parser = http_uri_parser_init(path);
1580 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01001581 if (!isttest(path))
Willy Tarreau79e57332018-10-02 16:01:16 +02001582 return 0;
1583
1584 smp->data.u.str.area = path.ptr;
1585 smp->data.u.str.data = path.len;
1586 smp->data.type = SMP_T_STR;
1587 smp->flags = SMP_F_CONST;
1588
1589 return 1;
1590}
1591
1592/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1593 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1594 */
1595static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1596{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001597 struct http_txn *txn;
1598
1599 if (!smp->strm)
1600 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001601
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001602 txn = smp->strm->txn;
Christopher Faulet09f88362021-04-01 16:00:29 +02001603 if (!txn || txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001604 return 0;
1605
1606 if (txn->req.flags & HTTP_MSGF_VER_11)
1607 smp->data.u.str.area = "HTTP/1.1";
1608 else
1609 smp->data.u.str.area = "HTTP/1.0";
1610
1611 smp->data.u.str.data = 8;
1612 smp->data.type = SMP_T_STR;
1613 smp->flags = SMP_F_CONST;
1614 return 1;
1615
1616}
1617
1618/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1619 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1620 */
1621static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1622{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001623 struct http_txn *txn;
1624
1625 if (!smp->strm)
1626 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001627
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001628 txn = smp->strm->txn;
Christopher Faulet09f88362021-04-01 16:00:29 +02001629 if (!txn || txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001630 return 0;
1631
1632 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1633 smp->data.u.str.area = "HTTP/1.1";
1634 else
1635 smp->data.u.str.area = "HTTP/1.0";
1636
1637 smp->data.u.str.data = 8;
1638 smp->data.type = SMP_T_STR;
1639 smp->flags = SMP_F_CONST;
1640 return 1;
1641
1642}
1643
1644/* Iterate over all cookies present in a message. The context is stored in
1645 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1646 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1647 * the direction, multiple cookies may be parsed on the same line or not.
Maciej Zdebdea7c202020-11-13 09:38:06 +00001648 * If provided, the searched cookie name is in args, in args->data.str. If
1649 * the input options indicate that no iterating is desired, then only last
1650 * value is fetched if any. If no cookie name is provided, the first cookie
1651 * value found is fetched. The returned sample is of type CSTR. Can be used
1652 * to parse cookies in other files.
Willy Tarreau79e57332018-10-02 16:01:16 +02001653 */
1654static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1655{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001656 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1657 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001658 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001659 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001660 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1661 struct ist hdr;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001662 char *cook = NULL;
1663 size_t cook_l = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001664 int found = 0;
1665
Christopher Faulet623af932021-01-29 11:22:15 +01001666 if (args->type == ARGT_STR) {
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001667 cook = args->data.str.area;
1668 cook_l = args->data.str.data;
1669 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001670
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001671 if (!ctx) {
1672 /* first call */
1673 ctx = &static_http_hdr_ctx;
1674 ctx->blk = NULL;
1675 smp->ctx.a[2] = ctx;
1676 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001677
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001678 if (!htx)
1679 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001680
Christopher Faulet16032ab2020-04-30 11:30:00 +02001681 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001682
Maciej Zdebdea7c202020-11-13 09:38:06 +00001683 /* OK so basically here, either we want only one value or we want to
1684 * iterate over all of them and we fetch the next one. In this last case
1685 * SMP_OPT_ITERATE option is set.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001686 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001687
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001688 if (!(smp->flags & SMP_F_NOT_LAST)) {
1689 /* search for the header from the beginning, we must first initialize
1690 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001691 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001692 smp->ctx.a[0] = NULL;
1693 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001694 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001695
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001696 smp->flags |= SMP_F_VOL_HDR;
1697 while (1) {
1698 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1699 if (!smp->ctx.a[0]) {
1700 if (!http_find_header(htx, hdr, ctx, 0))
1701 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001702
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001703 if (ctx->value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001704 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001705
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001706 smp->ctx.a[0] = ctx->value.ptr;
1707 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001708 }
1709
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001710 smp->data.type = SMP_T_STR;
1711 smp->flags |= SMP_F_CONST;
1712 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001713 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001714 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1715 &smp->data.u.str.area,
1716 &smp->data.u.str.data);
1717 if (smp->ctx.a[0]) {
1718 found = 1;
Maciej Zdebdea7c202020-11-13 09:38:06 +00001719 if (smp->opt & SMP_OPT_ITERATE) {
1720 /* iterate on cookie value */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001721 smp->flags |= SMP_F_NOT_LAST;
1722 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001723 }
Maciej Zdebdea7c202020-11-13 09:38:06 +00001724 if (args->data.str.data == 0) {
1725 /* No cookie name, first occurrence returned */
1726 break;
1727 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001728 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001729 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001730 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001731
Willy Tarreau79e57332018-10-02 16:01:16 +02001732 /* all cookie headers and values were scanned. If we're looking for the
1733 * last occurrence, we may return it now.
1734 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001735 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001736 smp->flags &= ~SMP_F_NOT_LAST;
1737 return found;
1738}
1739
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001740/* Same than smp_fetch_cookie() but only relies on the sample direction to
1741 * choose the right channel. So instead of duplicating the code, we just change
1742 * the keyword and then fallback on smp_fetch_cookie().
1743 */
1744static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1745{
1746 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1747 return smp_fetch_cookie(args, smp, kw, private);
1748}
1749
Willy Tarreau79e57332018-10-02 16:01:16 +02001750/* Iterate over all cookies present in a request to count how many occurrences
1751 * match the name in args and args->data.str.len. If <multi> is non-null, then
1752 * multiple cookies may be parsed on the same line. The returned sample is of
1753 * type UINT. Accepts exactly 1 argument of type string.
1754 */
1755static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1756{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001757 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1758 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001759 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001760 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001761 struct http_hdr_ctx ctx;
1762 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001763 char *val_beg, *val_end;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001764 char *cook = NULL;
1765 size_t cook_l = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001766 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001767
Christopher Faulet623af932021-01-29 11:22:15 +01001768 if (args->type == ARGT_STR){
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001769 cook = args->data.str.area;
1770 cook_l = args->data.str.data;
1771 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001772
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001773 if (!htx)
1774 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001775
Christopher Faulet16032ab2020-04-30 11:30:00 +02001776 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001777
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001778 val_end = val_beg = NULL;
1779 ctx.blk = NULL;
1780 cnt = 0;
1781 while (1) {
1782 /* Note: val_beg == NULL every time we need to fetch a new header */
1783 if (!val_beg) {
1784 if (!http_find_header(htx, hdr, &ctx, 0))
1785 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001786
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001787 if (ctx.value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001788 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001789
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001790 val_beg = ctx.value.ptr;
1791 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001792 }
1793
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001794 smp->data.type = SMP_T_STR;
1795 smp->flags |= SMP_F_CONST;
1796 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001797 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001798 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1799 &smp->data.u.str.area,
1800 &smp->data.u.str.data))) {
1801 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001802 }
1803 }
1804
1805 smp->data.type = SMP_T_SINT;
1806 smp->data.u.sint = cnt;
1807 smp->flags |= SMP_F_VOL_HDR;
1808 return 1;
1809}
1810
1811/* Fetch an cookie's integer value. The integer value is returned. It
1812 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1813 */
1814static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1815{
1816 int ret = smp_fetch_cookie(args, smp, kw, private);
1817
1818 if (ret > 0) {
1819 smp->data.type = SMP_T_SINT;
1820 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1821 smp->data.u.str.data);
1822 }
1823
1824 return ret;
1825}
1826
1827/************************************************************************/
1828/* The code below is dedicated to sample fetches */
1829/************************************************************************/
1830
1831/* This scans a URL-encoded query string. It takes an optionally wrapping
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001832 * string whose first contiguous chunk has its beginning in ctx->a[0] and end
Willy Tarreau79e57332018-10-02 16:01:16 +02001833 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1834 * pointers are updated for next iteration before leaving.
1835 */
Martin DOLEZ110e4a82023-03-28 09:06:05 -04001836static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private, char insensitive)
Willy Tarreau79e57332018-10-02 16:01:16 +02001837{
1838 const char *vstart, *vend;
1839 struct buffer *temp;
1840 const char **chunks = (const char **)smp->ctx.a;
1841
1842 if (!http_find_next_url_param(chunks, name, name_len,
Martin DOLEZ110e4a82023-03-28 09:06:05 -04001843 &vstart, &vend, delim, insensitive))
Willy Tarreau79e57332018-10-02 16:01:16 +02001844 return 0;
1845
1846 /* Create sample. If the value is contiguous, return the pointer as CONST,
1847 * if the value is wrapped, copy-it in a buffer.
1848 */
1849 smp->data.type = SMP_T_STR;
1850 if (chunks[2] &&
1851 vstart >= chunks[0] && vstart <= chunks[1] &&
1852 vend >= chunks[2] && vend <= chunks[3]) {
1853 /* Wrapped case. */
1854 temp = get_trash_chunk();
1855 memcpy(temp->area, vstart, chunks[1] - vstart);
1856 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1857 vend - chunks[2]);
1858 smp->data.u.str.area = temp->area;
1859 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1860 } else {
1861 /* Contiguous case. */
1862 smp->data.u.str.area = (char *)vstart;
1863 smp->data.u.str.data = vend - vstart;
1864 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1865 }
1866
1867 /* Update context, check wrapping. */
1868 chunks[0] = vend;
1869 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1870 chunks[1] = chunks[3];
1871 chunks[2] = NULL;
1872 }
1873
1874 if (chunks[0] < chunks[1])
1875 smp->flags |= SMP_F_NOT_LAST;
1876
1877 return 1;
1878}
1879
1880/* This function iterates over each parameter of the query string. It uses
1881 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1882 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1883 * An optional parameter name is passed in args[0], otherwise any parameter is
1884 * considered. It supports an optional delimiter argument for the beginning of
1885 * the string in args[1], which defaults to "?".
1886 */
1887static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1888{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001889 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001890 char delim = '?';
1891 const char *name;
1892 int name_len;
Martin DOLEZ28c5f402023-03-28 09:06:05 -04001893 char insensitive = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001894
Christopher Faulet623af932021-01-29 11:22:15 +01001895 if ((args[0].type && args[0].type != ARGT_STR) ||
Martin DOLEZ28c5f402023-03-28 09:06:05 -04001896 (args[1].type && args[1].type != ARGT_STR) ||
1897 (args[2].type && args[2].type != ARGT_STR))
Willy Tarreau79e57332018-10-02 16:01:16 +02001898 return 0;
1899
1900 name = "";
1901 name_len = 0;
1902 if (args->type == ARGT_STR) {
1903 name = args->data.str.area;
1904 name_len = args->data.str.data;
1905 }
1906
Martin DOLEZ1a9a9942023-03-28 09:49:53 -04001907 if (args[1].type && *args[1].data.str.area)
Willy Tarreau79e57332018-10-02 16:01:16 +02001908 delim = *args[1].data.str.area;
Martin DOLEZ28c5f402023-03-28 09:06:05 -04001909 if (args[2].type && *args[2].data.str.area == 'i')
1910 insensitive = 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001911
1912 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001913 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001914 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001915
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001916 if (!htx)
1917 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001918
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001919 sl = http_get_stline(htx);
1920 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1921 if (!smp->ctx.a[0])
1922 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001923
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001924 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001925
1926 /* Assume that the context is filled with NULL pointer
1927 * before the first call.
1928 * smp->ctx.a[2] = NULL;
1929 * smp->ctx.a[3] = NULL;
1930 */
1931 }
1932
Martin DOLEZ28c5f402023-03-28 09:06:05 -04001933 return smp_fetch_param(delim, name, name_len, args, smp, kw, private, insensitive);
Willy Tarreau79e57332018-10-02 16:01:16 +02001934}
1935
1936/* This function iterates over each parameter of the body. This requires
1937 * that the body has been waited for using http-buffer-request. It uses
1938 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001939 * contiguous part of the body, and optionally ctx->a[2..3] to reference the
Willy Tarreau79e57332018-10-02 16:01:16 +02001940 * optional second part if the body wraps at the end of the buffer. An optional
1941 * parameter name is passed in args[0], otherwise any parameter is considered.
1942 */
1943static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1944{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001945 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001946 const char *name;
1947 int name_len;
Martin DOLEZ28c5f402023-03-28 09:06:05 -04001948 char insensitive = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001949
Martin DOLEZ28c5f402023-03-28 09:06:05 -04001950 if ((args[0].type && args[0].type != ARGT_STR) ||
1951 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau79e57332018-10-02 16:01:16 +02001952 return 0;
1953
1954 name = "";
1955 name_len = 0;
1956 if (args[0].type == ARGT_STR) {
1957 name = args[0].data.str.area;
1958 name_len = args[0].data.str.data;
1959 }
1960
Martin DOLEZ28c5f402023-03-28 09:06:05 -04001961 if (args[1].type && *args[1].data.str.area == 'i')
1962 insensitive = 1;
1963
Willy Tarreau79e57332018-10-02 16:01:16 +02001964 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulete596d182020-05-05 17:46:34 +02001965 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001966 struct buffer *temp;
1967 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001968
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001969 if (!htx)
1970 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001971
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001972 temp = get_trash_chunk();
1973 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1974 struct htx_blk *blk = htx_get_blk(htx, pos);
1975 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001976
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001977 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001978 break;
1979 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001980 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001981 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001982 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001983 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001984
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001985 smp->ctx.a[0] = temp->area;
1986 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001987
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001988 /* Assume that the context is filled with NULL pointer
1989 * before the first call.
1990 * smp->ctx.a[2] = NULL;
1991 * smp->ctx.a[3] = NULL;
1992 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001993
Willy Tarreau79e57332018-10-02 16:01:16 +02001994 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001995
Martin DOLEZ28c5f402023-03-28 09:06:05 -04001996 return smp_fetch_param('&', name, name_len, args, smp, kw, private, insensitive);
Willy Tarreau79e57332018-10-02 16:01:16 +02001997}
1998
1999/* Return the signed integer value for the specified url parameter (see url_param
2000 * above).
2001 */
2002static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
2003{
2004 int ret = smp_fetch_url_param(args, smp, kw, private);
2005
2006 if (ret > 0) {
2007 smp->data.type = SMP_T_SINT;
2008 smp->data.u.sint = strl2ic(smp->data.u.str.area,
2009 smp->data.u.str.data);
2010 }
2011
2012 return ret;
2013}
2014
2015/* This produces a 32-bit hash of the concatenation of the first occurrence of
2016 * the Host header followed by the path component if it begins with a slash ('/').
2017 * This means that '*' will not be added, resulting in exactly the first Host
2018 * entry. If no Host header is found, then the path is used. The resulting value
2019 * is hashed using the url hash followed by a full avalanche hash and provides a
2020 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2021 * high-traffic sites without having to store whole paths.
2022 * this differs from the base32 functions in that it includes the url parameters
2023 * as well as the path
2024 */
2025static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2026{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002027 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002028 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02002029 struct http_hdr_ctx ctx;
2030 struct htx_sl *sl;
2031 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002032 unsigned int hash = 0;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002033 struct http_uri_parser parser;
Willy Tarreau79e57332018-10-02 16:01:16 +02002034
Christopher Faulet6d1dd462019-07-15 14:36:03 +02002035 if (!htx)
2036 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002037
Christopher Faulet6d1dd462019-07-15 14:36:03 +02002038 ctx.blk = NULL;
2039 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2040 /* OK we have the header value in ctx.value */
2041 while (ctx.value.len--)
2042 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02002043 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002044
Christopher Faulet6d1dd462019-07-15 14:36:03 +02002045 /* now retrieve the path */
2046 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002047 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2048 path = http_parse_path(&parser);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02002049 if (path.len && *(path.ptr) == '/') {
2050 while (path.len--)
2051 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02002052 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002053
Willy Tarreau79e57332018-10-02 16:01:16 +02002054 hash = full_hash(hash);
2055
2056 smp->data.type = SMP_T_SINT;
2057 smp->data.u.sint = hash;
2058 smp->flags = SMP_F_VOL_1ST;
2059 return 1;
2060}
2061
2062/* This concatenates the source address with the 32-bit hash of the Host and
2063 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2064 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2065 * on the source address length. The URL hash is stored before the address so
2066 * that in environments where IPv6 is insignificant, truncating the output to
2067 * 8 bytes would still work.
2068 */
2069static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2070{
Willy Tarreaud68ff012022-05-27 08:57:21 +02002071 const struct sockaddr_storage *src = (smp->strm ? sc_src(smp->strm->scf) : NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +02002072 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02002073
Christopher Faulet6fc817a2021-10-25 07:48:27 +02002074 if (!src)
Willy Tarreau79e57332018-10-02 16:01:16 +02002075 return 0;
2076
2077 if (!smp_fetch_url32(args, smp, kw, private))
2078 return 0;
2079
2080 temp = get_trash_chunk();
2081 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2082 temp->data += sizeof(unsigned int);
2083
Christopher Faulet6fc817a2021-10-25 07:48:27 +02002084 switch (src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02002085 case AF_INET:
2086 memcpy(temp->area + temp->data,
Christopher Faulet6fc817a2021-10-25 07:48:27 +02002087 &((struct sockaddr_in *)src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002088 4);
2089 temp->data += 4;
2090 break;
2091 case AF_INET6:
2092 memcpy(temp->area + temp->data,
Christopher Faulet6fc817a2021-10-25 07:48:27 +02002093 &((struct sockaddr_in6 *)src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002094 16);
2095 temp->data += 16;
2096 break;
2097 default:
2098 return 0;
2099 }
2100
2101 smp->data.u.str = *temp;
2102 smp->data.type = SMP_T_BIN;
2103 return 1;
2104}
2105
2106/************************************************************************/
2107/* Other utility functions */
2108/************************************************************************/
2109
2110/* This function is used to validate the arguments passed to any "hdr" fetch
2111 * keyword. These keywords support an optional positive or negative occurrence
2112 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2113 * is assumed that the types are already the correct ones. Returns 0 on error,
2114 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2115 * error message in case of error, that the caller is responsible for freeing.
2116 * The initial location must either be freeable or NULL.
2117 * Note: this function's pointer is checked from Lua.
2118 */
2119int val_hdr(struct arg *arg, char **err_msg)
2120{
2121 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2122 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2123 return 0;
2124 }
2125 return 1;
2126}
2127
2128/************************************************************************/
2129/* All supported sample fetch keywords must be declared here. */
2130/************************************************************************/
2131
2132/* Note: must not be declared <const> as its list will be overwritten */
2133static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2134 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2135 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2136 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Yves Lafonb4d37082021-02-11 11:01:28 +01002137 { "baseq", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002138
2139 /* capture are allocated and are permanent in the stream */
2140 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2141
2142 /* retrieve these captures from the HTTP logs */
2143 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2144 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2145 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2146
2147 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2148 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2149
2150 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2151 * are only here to match the ACL's name, are request-only and are used
2152 * for ACL compatibility only.
2153 */
2154 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002155 { "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 +02002156 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2157 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2158
2159 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2160 * only here to match the ACL's name, are request-only and are used for
2161 * ACL compatibility only.
2162 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002163 { "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 +02002164 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2165 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2166 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2167
Christopher Fauleta4063562019-08-02 11:51:37 +02002168 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2169 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2170 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Remi Tricot-Le Bretonf5dd3372021-10-01 15:36:53 +02002171 { "http_auth_bearer", smp_fetch_http_auth_bearer, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002172 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2173 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2174 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2175 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2176 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Faulete720c322020-09-02 17:25:18 +02002177 { "pathq", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002178 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2179
2180 /* HTTP protocol on the request path */
2181 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2182 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2183
2184 /* HTTP version on the request path */
2185 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2186 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2187
2188 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2189 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2190 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Martin DOLEZ28c5f402023-03-28 09:06:05 -04002191 { "req.body_param", smp_fetch_body_param, ARG2(0,STR,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002192
2193 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2194 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2195
2196 /* HTTP version on the response path */
2197 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2198 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2199
Christopher Faulete596d182020-05-05 17:46:34 +02002200 { "res.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2201 { "res.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2202 { "res.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2203
2204 { "res.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2205 { "res.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2206
Willy Tarreau79e57332018-10-02 16:01:16 +02002207 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2208 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2209 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2210 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2211
2212 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2213 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2214 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2215 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2216 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2217 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2218 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2219
2220 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2221 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2222 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2223 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2224
2225 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2226 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2227 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2228 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2229 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2230 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2231 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2232
2233 /* scook is valid only on the response and is used for ACL compatibility */
2234 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2235 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2236 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002237
2238 /* shdr is valid only on the response and is used for ACL compatibility */
2239 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2240 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2241 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2242 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2243
2244 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2245 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2246 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2247 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2248 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2249 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2250 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
Martin DOLEZ28c5f402023-03-28 09:06:05 -04002251 { "url_param", smp_fetch_url_param, ARG3(0,STR,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2252 { "urlp" , smp_fetch_url_param, ARG3(0,STR,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2253 { "urlp_val", smp_fetch_url_param_val, ARG3(0,STR,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Christopher Faulet16032ab2020-04-30 11:30:00 +02002254
Willy Tarreau79e57332018-10-02 16:01:16 +02002255 { /* END */ },
2256}};
2257
Willy Tarreau0108d902018-11-25 19:14:37 +01002258INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002259
2260/*
2261 * Local variables:
2262 * c-indent-level: 8
2263 * c-basic-offset: 8
2264 * End:
2265 */