blob: abb19a4554533549dd87601db963642385c8ca0d [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 Faulet89dc4992019-04-17 12:02:59 +0200229 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
230 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 Faulet89dc4992019-04-17 12:02:59 +0200314 else
315 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) &&
618 !(chn->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)))))
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 Faulet6d1dd462019-07-15 14:36:03 +0200682 if (htx->extra != ULLONG_MAX)
683 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;
1027 } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1028 smp->data.type = SMP_T_IPV6;
1029 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001030 }
1031 }
1032
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001033 /* if the header doesn't match an IP address, fetch next one */
1034 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001035 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001036 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001037 return ret;
1038}
Willy Tarreau79e57332018-10-02 16:01:16 +02001039
Christopher Faulete720c322020-09-02 17:25:18 +02001040/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at the
1041 * first '/' after the possible hostname. It ends before the possible '?' except
1042 * for 'pathq' keyword.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001043 */
1044static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1045{
1046 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001047 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001048 struct htx_sl *sl;
1049 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02001050 struct http_uri_parser parser;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001051
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001052 if (!htx)
1053 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001054
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001055 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02001056 parser = http_uri_parser_init(htx_sl_req_uri(sl));
Christopher Faulete720c322020-09-02 17:25:18 +02001057
Yves Lafonb4d37082021-02-11 11:01:28 +01001058 if (kw[4] == 'q' && (kw[0] == 'p' || kw[0] == 'b')) // pathq or baseq
Amaury Denoyellec453f952021-07-06 11:40:12 +02001059 path = http_parse_path(&parser);
Christopher Faulete720c322020-09-02 17:25:18 +02001060 else
Amaury Denoyellec453f952021-07-06 11:40:12 +02001061 path = iststop(http_parse_path(&parser), '?');
Christopher Faulete720c322020-09-02 17:25:18 +02001062
Tim Duesterhused526372020-03-05 17:56:33 +01001063 if (!isttest(path))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001064 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001065
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001066 /* OK, we got the '/' ! */
1067 smp->data.type = SMP_T_STR;
1068 smp->data.u.str.area = path.ptr;
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001069 smp->data.u.str.data = path.len;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001070 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001071 return 1;
1072}
1073
1074/* This produces a concatenation of the first occurrence of the Host header
1075 * followed by the path component if it begins with a slash ('/'). This means
1076 * that '*' will not be added, resulting in exactly the first Host entry.
1077 * If no Host header is found, then the path is returned as-is. The returned
1078 * value is stored in the trash so it does not need to be marked constant.
1079 * The returned sample is of type string.
1080 */
1081static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1082{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001083 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001084 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001085 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001086 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001087 struct http_hdr_ctx ctx;
1088 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02001089 struct http_uri_parser parser;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001090
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001091 if (!htx)
1092 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001093
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001094 ctx.blk = NULL;
1095 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1096 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001097
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001098 /* OK we have the header value in ctx.value */
1099 temp = get_trash_chunk();
Tim Duesterhus77508502022-03-15 13:11:06 +01001100 chunk_istcat(temp, ctx.value);
Willy Tarreau79e57332018-10-02 16:01:16 +02001101
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001102 /* now retrieve the path */
1103 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02001104 parser = http_uri_parser_init(htx_sl_req_uri(sl));
1105 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01001106 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001107 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001108
Yves Lafonb4d37082021-02-11 11:01:28 +01001109 if (kw[4] == 'q' && kw[0] == 'b') { // baseq
1110 len = path.len;
1111 } else {
1112 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1113 ;
1114 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001115
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001116 if (len && *(path.ptr) == '/')
1117 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001118 }
1119
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001120 smp->data.type = SMP_T_STR;
1121 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001122 smp->flags = SMP_F_VOL_1ST;
1123 return 1;
1124}
1125
1126/* This produces a 32-bit hash of the concatenation of the first occurrence of
1127 * the Host header followed by the path component if it begins with a slash ('/').
1128 * This means that '*' will not be added, resulting in exactly the first Host
1129 * entry. If no Host header is found, then the path is used. The resulting value
1130 * is hashed using the path hash followed by a full avalanche hash and provides a
1131 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1132 * high-traffic sites without having to store whole paths.
1133 */
1134static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1135{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001136 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001137 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001138 struct htx_sl *sl;
1139 struct http_hdr_ctx ctx;
1140 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001141 unsigned int hash = 0;
Amaury Denoyellec453f952021-07-06 11:40:12 +02001142 struct http_uri_parser parser;
Willy Tarreau79e57332018-10-02 16:01:16 +02001143
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001144 if (!htx)
1145 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001146
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001147 ctx.blk = NULL;
1148 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1149 /* OK we have the header value in ctx.value */
1150 while (ctx.value.len--)
1151 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001152 }
1153
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001154 /* now retrieve the path */
1155 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02001156 parser = http_uri_parser_init(htx_sl_req_uri(sl));
1157 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01001158 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001159 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001160
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001161 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1162 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001163
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001164 if (len && *(path.ptr) == '/') {
1165 while (len--)
1166 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001167 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001168 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001169
Willy Tarreau79e57332018-10-02 16:01:16 +02001170 hash = full_hash(hash);
1171
1172 smp->data.type = SMP_T_SINT;
1173 smp->data.u.sint = hash;
1174 smp->flags = SMP_F_VOL_1ST;
1175 return 1;
1176}
1177
1178/* This concatenates the source address with the 32-bit hash of the Host and
1179 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1180 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1181 * on the source address length. The path hash is stored before the address so
1182 * that in environments where IPv6 is insignificant, truncating the output to
1183 * 8 bytes would still work.
1184 */
1185static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1186{
Willy Tarreaud68ff012022-05-27 08:57:21 +02001187 const struct sockaddr_storage *src = (smp->strm ? sc_src(smp->strm->scf) : NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +02001188 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001189
Christopher Faulet6fc817a2021-10-25 07:48:27 +02001190 if (!src)
Willy Tarreau79e57332018-10-02 16:01:16 +02001191 return 0;
1192
1193 if (!smp_fetch_base32(args, smp, kw, private))
1194 return 0;
1195
1196 temp = get_trash_chunk();
1197 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1198 temp->data += sizeof(unsigned int);
1199
Christopher Faulet6fc817a2021-10-25 07:48:27 +02001200 switch (src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001201 case AF_INET:
1202 memcpy(temp->area + temp->data,
Christopher Faulet6fc817a2021-10-25 07:48:27 +02001203 &((struct sockaddr_in *)src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001204 4);
1205 temp->data += 4;
1206 break;
1207 case AF_INET6:
1208 memcpy(temp->area + temp->data,
Christopher Faulet6fc817a2021-10-25 07:48:27 +02001209 &((struct sockaddr_in6 *)src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001210 16);
1211 temp->data += 16;
1212 break;
1213 default:
1214 return 0;
1215 }
1216
1217 smp->data.u.str = *temp;
1218 smp->data.type = SMP_T_BIN;
1219 return 1;
1220}
1221
1222/* Extracts the query string, which comes after the question mark '?'. If no
1223 * question mark is found, nothing is returned. Otherwise it returns a sample
1224 * of type string carrying the whole query string.
1225 */
1226static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1227{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001228 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001229 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001230 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001231 char *ptr, *end;
1232
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001233 if (!htx)
1234 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001235
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001236 sl = http_get_stline(htx);
1237 ptr = HTX_SL_REQ_UPTR(sl);
1238 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001239
1240 /* look up the '?' */
1241 do {
1242 if (ptr == end)
1243 return 0;
1244 } while (*ptr++ != '?');
1245
1246 smp->data.type = SMP_T_STR;
1247 smp->data.u.str.area = ptr;
1248 smp->data.u.str.data = end - ptr;
1249 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1250 return 1;
1251}
1252
1253static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1254{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001255 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001256 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001257
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001258 if (!htx)
1259 return 0;
1260 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001261 smp->data.u.sint = 1;
1262 return 1;
1263}
1264
1265/* return a valid test if the current request is the first one on the connection */
1266static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1267{
Willy Tarreau79512b62020-04-29 11:52:13 +02001268 if (!smp->strm)
1269 return 0;
1270
Willy Tarreau79e57332018-10-02 16:01:16 +02001271 smp->data.type = SMP_T_BOOL;
1272 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1273 return 1;
1274}
1275
Christopher Fauleta4063562019-08-02 11:51:37 +02001276/* Fetch the authentication method if there is an Authorization header. It
1277 * relies on get_http_auth()
1278 */
1279static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1280{
1281 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001282 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001283 struct http_txn *txn;
1284
1285 if (!htx)
1286 return 0;
1287
1288 txn = smp->strm->txn;
1289 if (!get_http_auth(smp, htx))
1290 return 0;
1291
1292 switch (txn->auth.method) {
1293 case HTTP_AUTH_BASIC:
1294 smp->data.u.str.area = "Basic";
1295 smp->data.u.str.data = 5;
1296 break;
1297 case HTTP_AUTH_DIGEST:
1298 /* Unexpected because not supported */
1299 smp->data.u.str.area = "Digest";
1300 smp->data.u.str.data = 6;
1301 break;
Remi Tricot-Le Bretonf5dd3372021-10-01 15:36:53 +02001302 case HTTP_AUTH_BEARER:
1303 smp->data.u.str.area = "Bearer";
1304 smp->data.u.str.data = 6;
1305 break;
Christopher Fauleta4063562019-08-02 11:51:37 +02001306 default:
1307 return 0;
1308 }
1309
1310 smp->data.type = SMP_T_STR;
1311 smp->flags = SMP_F_CONST;
1312 return 1;
1313}
1314
1315/* Fetch the user supplied if there is an Authorization header. It relies on
1316 * get_http_auth()
1317 */
1318static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1319{
1320 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001321 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001322 struct http_txn *txn;
1323
1324 if (!htx)
1325 return 0;
1326
1327 txn = smp->strm->txn;
Remi Tricot-Le Bretonf5dd3372021-10-01 15:36:53 +02001328 if (!get_http_auth(smp, htx) || txn->auth.method != HTTP_AUTH_BASIC)
Christopher Fauleta4063562019-08-02 11:51:37 +02001329 return 0;
1330
1331 smp->data.type = SMP_T_STR;
1332 smp->data.u.str.area = txn->auth.user;
1333 smp->data.u.str.data = strlen(txn->auth.user);
1334 smp->flags = SMP_F_CONST;
1335 return 1;
1336}
1337
1338/* Fetch the password supplied if there is an Authorization header. It relies on
1339 * get_http_auth()
1340 */
1341static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1342{
1343 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001344 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001345 struct http_txn *txn;
1346
1347 if (!htx)
1348 return 0;
1349
1350 txn = smp->strm->txn;
Remi Tricot-Le Bretonf5dd3372021-10-01 15:36:53 +02001351 if (!get_http_auth(smp, htx) || txn->auth.method != HTTP_AUTH_BASIC)
Christopher Fauleta4063562019-08-02 11:51:37 +02001352 return 0;
1353
1354 smp->data.type = SMP_T_STR;
1355 smp->data.u.str.area = txn->auth.pass;
1356 smp->data.u.str.data = strlen(txn->auth.pass);
1357 smp->flags = SMP_F_CONST;
1358 return 1;
1359}
1360
Remi Tricot-Le Bretonf5dd3372021-10-01 15:36:53 +02001361static int smp_fetch_http_auth_bearer(const struct arg *args, struct sample *smp, const char *kw, void *private)
1362{
1363 struct channel *chn = SMP_REQ_CHN(smp);
1364 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
1365 struct http_txn *txn;
1366 struct buffer bearer_val = {};
1367
1368 if (!htx)
1369 return 0;
1370
1371 if (args->type == ARGT_STR) {
1372 struct http_hdr_ctx ctx;
1373 struct ist hdr_name = ist2(args->data.str.area, args->data.str.data);
1374
1375 ctx.blk = NULL;
1376 if (http_find_header(htx, hdr_name, &ctx, 0)) {
Remi Tricot-Le Breton7da35bf2021-10-29 15:25:19 +02001377 struct ist type = istsplit(&ctx.value, ' ');
1378
1379 /* There must be "at least" one space character between
1380 * the scheme and the following value so ctx.value might
1381 * still have leading spaces here (see RFC7235).
1382 */
1383 ctx.value = istskip(ctx.value, ' ');
1384
1385 if (isteqi(type, ist("Bearer")) && istlen(ctx.value))
1386 chunk_initlen(&bearer_val, istptr(ctx.value), 0, istlen(ctx.value));
Remi Tricot-Le Bretonf5dd3372021-10-01 15:36:53 +02001387 }
1388 }
1389 else {
1390 txn = smp->strm->txn;
1391 if (!get_http_auth(smp, htx) || txn->auth.method != HTTP_AUTH_BEARER)
1392 return 0;
1393
1394 bearer_val = txn->auth.method_data;
1395 }
1396
1397 smp->data.type = SMP_T_STR;
1398 smp->data.u.str = bearer_val;
1399 smp->flags = SMP_F_CONST;
1400 return 1;
1401}
1402
Willy Tarreau79e57332018-10-02 16:01:16 +02001403/* Accepts exactly 1 argument of type userlist */
1404static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1405{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001406 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001407 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001408
Christopher Faulet623af932021-01-29 11:22:15 +01001409 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001410 return 0;
1411
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001412 if (!htx)
1413 return 0;
Remi Tricot-Le Bretonf5dd3372021-10-01 15:36:53 +02001414 if (!get_http_auth(smp, htx) || smp->strm->txn->auth.method != HTTP_AUTH_BASIC)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001415 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001416
1417 smp->data.type = SMP_T_BOOL;
1418 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001419 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001420 return 1;
1421}
1422
1423/* Accepts exactly 1 argument of type userlist */
1424static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1425{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001426 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001427 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001428
Christopher Faulet623af932021-01-29 11:22:15 +01001429 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001430 return 0;
1431
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001432 if (!htx)
1433 return 0;
Remi Tricot-Le Bretonf5dd3372021-10-01 15:36:53 +02001434 if (!get_http_auth(smp, htx) || smp->strm->txn->auth.method != HTTP_AUTH_BASIC)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001435 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001436
Willy Tarreau79e57332018-10-02 16:01:16 +02001437 /* if the user does not belong to the userlist or has a wrong password,
1438 * report that it unconditionally does not match. Otherwise we return
1439 * a string containing the username.
1440 */
1441 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1442 smp->strm->txn->auth.pass))
1443 return 0;
1444
1445 /* pat_match_auth() will need the user list */
1446 smp->ctx.a[0] = args->data.usr;
1447
1448 smp->data.type = SMP_T_STR;
1449 smp->flags = SMP_F_CONST;
1450 smp->data.u.str.area = smp->strm->txn->auth.user;
1451 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1452
1453 return 1;
1454}
1455
1456/* Fetch a captured HTTP request header. The index is the position of
1457 * the "capture" option in the configuration file
1458 */
1459static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1460{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001461 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001462 int idx;
1463
Christopher Faulet623af932021-01-29 11:22:15 +01001464 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001465 return 0;
1466
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001467 if (!smp->strm)
1468 return 0;
1469
1470 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001471 idx = args->data.sint;
1472
1473 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1474 return 0;
1475
1476 smp->data.type = SMP_T_STR;
1477 smp->flags |= SMP_F_CONST;
1478 smp->data.u.str.area = smp->strm->req_cap[idx];
1479 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1480
1481 return 1;
1482}
1483
1484/* Fetch a captured HTTP response header. The index is the position of
1485 * the "capture" option in the configuration file
1486 */
1487static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1488{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001489 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001490 int idx;
1491
Christopher Faulet623af932021-01-29 11:22:15 +01001492 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001493 return 0;
1494
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001495 if (!smp->strm)
1496 return 0;
1497
1498 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001499 idx = args->data.sint;
1500
1501 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1502 return 0;
1503
1504 smp->data.type = SMP_T_STR;
1505 smp->flags |= SMP_F_CONST;
1506 smp->data.u.str.area = smp->strm->res_cap[idx];
1507 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1508
1509 return 1;
1510}
1511
1512/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1513static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1514{
1515 struct buffer *temp;
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001516 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001517 char *ptr;
1518
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001519 if (!smp->strm)
1520 return 0;
1521
1522 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001523 if (!txn || !txn->uri)
1524 return 0;
1525
1526 ptr = txn->uri;
1527
1528 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1529 ptr++;
1530
1531 temp = get_trash_chunk();
1532 temp->area = txn->uri;
1533 temp->data = ptr - txn->uri;
1534 smp->data.u.str = *temp;
1535 smp->data.type = SMP_T_STR;
1536 smp->flags = SMP_F_CONST;
1537
1538 return 1;
1539
1540}
1541
1542/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1543static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1544{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001545 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001546 struct ist path;
1547 const char *ptr;
Amaury Denoyellec453f952021-07-06 11:40:12 +02001548 struct http_uri_parser parser;
Willy Tarreau79e57332018-10-02 16:01:16 +02001549
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001550 if (!smp->strm)
1551 return 0;
1552
1553 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001554 if (!txn || !txn->uri)
1555 return 0;
1556
1557 ptr = txn->uri;
1558
1559 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1560 ptr++;
1561
1562 if (!*ptr)
1563 return 0;
1564
Christopher Faulet78337bb2018-11-15 14:35:18 +01001565 /* skip the first space and find space after URI */
1566 path = ist2(++ptr, 0);
1567 while (*ptr != ' ' && *ptr != '\0')
1568 ptr++;
1569 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001570
Amaury Denoyellec453f952021-07-06 11:40:12 +02001571 parser = http_uri_parser_init(path);
1572 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01001573 if (!isttest(path))
Willy Tarreau79e57332018-10-02 16:01:16 +02001574 return 0;
1575
1576 smp->data.u.str.area = path.ptr;
1577 smp->data.u.str.data = path.len;
1578 smp->data.type = SMP_T_STR;
1579 smp->flags = SMP_F_CONST;
1580
1581 return 1;
1582}
1583
1584/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1585 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1586 */
1587static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1588{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001589 struct http_txn *txn;
1590
1591 if (!smp->strm)
1592 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001593
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001594 txn = smp->strm->txn;
Christopher Faulet09f88362021-04-01 16:00:29 +02001595 if (!txn || txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001596 return 0;
1597
1598 if (txn->req.flags & HTTP_MSGF_VER_11)
1599 smp->data.u.str.area = "HTTP/1.1";
1600 else
1601 smp->data.u.str.area = "HTTP/1.0";
1602
1603 smp->data.u.str.data = 8;
1604 smp->data.type = SMP_T_STR;
1605 smp->flags = SMP_F_CONST;
1606 return 1;
1607
1608}
1609
1610/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1611 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1612 */
1613static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1614{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001615 struct http_txn *txn;
1616
1617 if (!smp->strm)
1618 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001619
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001620 txn = smp->strm->txn;
Christopher Faulet09f88362021-04-01 16:00:29 +02001621 if (!txn || txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001622 return 0;
1623
1624 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1625 smp->data.u.str.area = "HTTP/1.1";
1626 else
1627 smp->data.u.str.area = "HTTP/1.0";
1628
1629 smp->data.u.str.data = 8;
1630 smp->data.type = SMP_T_STR;
1631 smp->flags = SMP_F_CONST;
1632 return 1;
1633
1634}
1635
1636/* Iterate over all cookies present in a message. The context is stored in
1637 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1638 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1639 * the direction, multiple cookies may be parsed on the same line or not.
Maciej Zdebdea7c202020-11-13 09:38:06 +00001640 * If provided, the searched cookie name is in args, in args->data.str. If
1641 * the input options indicate that no iterating is desired, then only last
1642 * value is fetched if any. If no cookie name is provided, the first cookie
1643 * value found is fetched. The returned sample is of type CSTR. Can be used
1644 * to parse cookies in other files.
Willy Tarreau79e57332018-10-02 16:01:16 +02001645 */
1646static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1647{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001648 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1649 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001650 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001651 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001652 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1653 struct ist hdr;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001654 char *cook = NULL;
1655 size_t cook_l = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001656 int found = 0;
1657
Christopher Faulet623af932021-01-29 11:22:15 +01001658 if (args->type == ARGT_STR) {
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001659 cook = args->data.str.area;
1660 cook_l = args->data.str.data;
1661 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001662
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001663 if (!ctx) {
1664 /* first call */
1665 ctx = &static_http_hdr_ctx;
1666 ctx->blk = NULL;
1667 smp->ctx.a[2] = ctx;
1668 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001669
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001670 if (!htx)
1671 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001672
Christopher Faulet16032ab2020-04-30 11:30:00 +02001673 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001674
Maciej Zdebdea7c202020-11-13 09:38:06 +00001675 /* OK so basically here, either we want only one value or we want to
1676 * iterate over all of them and we fetch the next one. In this last case
1677 * SMP_OPT_ITERATE option is set.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001678 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001679
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001680 if (!(smp->flags & SMP_F_NOT_LAST)) {
1681 /* search for the header from the beginning, we must first initialize
1682 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001683 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001684 smp->ctx.a[0] = NULL;
1685 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001686 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001687
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001688 smp->flags |= SMP_F_VOL_HDR;
1689 while (1) {
1690 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1691 if (!smp->ctx.a[0]) {
1692 if (!http_find_header(htx, hdr, ctx, 0))
1693 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001694
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001695 if (ctx->value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001696 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001697
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001698 smp->ctx.a[0] = ctx->value.ptr;
1699 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001700 }
1701
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001702 smp->data.type = SMP_T_STR;
1703 smp->flags |= SMP_F_CONST;
1704 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001705 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001706 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1707 &smp->data.u.str.area,
1708 &smp->data.u.str.data);
1709 if (smp->ctx.a[0]) {
1710 found = 1;
Maciej Zdebdea7c202020-11-13 09:38:06 +00001711 if (smp->opt & SMP_OPT_ITERATE) {
1712 /* iterate on cookie value */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001713 smp->flags |= SMP_F_NOT_LAST;
1714 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001715 }
Maciej Zdebdea7c202020-11-13 09:38:06 +00001716 if (args->data.str.data == 0) {
1717 /* No cookie name, first occurrence returned */
1718 break;
1719 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001720 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001721 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001722 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001723
Willy Tarreau79e57332018-10-02 16:01:16 +02001724 /* all cookie headers and values were scanned. If we're looking for the
1725 * last occurrence, we may return it now.
1726 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001727 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001728 smp->flags &= ~SMP_F_NOT_LAST;
1729 return found;
1730}
1731
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001732/* Same than smp_fetch_cookie() but only relies on the sample direction to
1733 * choose the right channel. So instead of duplicating the code, we just change
1734 * the keyword and then fallback on smp_fetch_cookie().
1735 */
1736static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1737{
1738 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1739 return smp_fetch_cookie(args, smp, kw, private);
1740}
1741
Willy Tarreau79e57332018-10-02 16:01:16 +02001742/* Iterate over all cookies present in a request to count how many occurrences
1743 * match the name in args and args->data.str.len. If <multi> is non-null, then
1744 * multiple cookies may be parsed on the same line. The returned sample is of
1745 * type UINT. Accepts exactly 1 argument of type string.
1746 */
1747static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1748{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001749 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1750 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001751 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001752 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001753 struct http_hdr_ctx ctx;
1754 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001755 char *val_beg, *val_end;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001756 char *cook = NULL;
1757 size_t cook_l = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001758 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001759
Christopher Faulet623af932021-01-29 11:22:15 +01001760 if (args->type == ARGT_STR){
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001761 cook = args->data.str.area;
1762 cook_l = args->data.str.data;
1763 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001764
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001765 if (!htx)
1766 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001767
Christopher Faulet16032ab2020-04-30 11:30:00 +02001768 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001769
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001770 val_end = val_beg = NULL;
1771 ctx.blk = NULL;
1772 cnt = 0;
1773 while (1) {
1774 /* Note: val_beg == NULL every time we need to fetch a new header */
1775 if (!val_beg) {
1776 if (!http_find_header(htx, hdr, &ctx, 0))
1777 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001778
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001779 if (ctx.value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001780 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001781
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001782 val_beg = ctx.value.ptr;
1783 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001784 }
1785
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001786 smp->data.type = SMP_T_STR;
1787 smp->flags |= SMP_F_CONST;
1788 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001789 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001790 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1791 &smp->data.u.str.area,
1792 &smp->data.u.str.data))) {
1793 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001794 }
1795 }
1796
1797 smp->data.type = SMP_T_SINT;
1798 smp->data.u.sint = cnt;
1799 smp->flags |= SMP_F_VOL_HDR;
1800 return 1;
1801}
1802
1803/* Fetch an cookie's integer value. The integer value is returned. It
1804 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1805 */
1806static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1807{
1808 int ret = smp_fetch_cookie(args, smp, kw, private);
1809
1810 if (ret > 0) {
1811 smp->data.type = SMP_T_SINT;
1812 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1813 smp->data.u.str.data);
1814 }
1815
1816 return ret;
1817}
1818
1819/************************************************************************/
1820/* The code below is dedicated to sample fetches */
1821/************************************************************************/
1822
1823/* This scans a URL-encoded query string. It takes an optionally wrapping
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001824 * string whose first contiguous chunk has its beginning in ctx->a[0] and end
Willy Tarreau79e57332018-10-02 16:01:16 +02001825 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1826 * pointers are updated for next iteration before leaving.
1827 */
1828static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1829{
1830 const char *vstart, *vend;
1831 struct buffer *temp;
1832 const char **chunks = (const char **)smp->ctx.a;
1833
1834 if (!http_find_next_url_param(chunks, name, name_len,
1835 &vstart, &vend, delim))
1836 return 0;
1837
1838 /* Create sample. If the value is contiguous, return the pointer as CONST,
1839 * if the value is wrapped, copy-it in a buffer.
1840 */
1841 smp->data.type = SMP_T_STR;
1842 if (chunks[2] &&
1843 vstart >= chunks[0] && vstart <= chunks[1] &&
1844 vend >= chunks[2] && vend <= chunks[3]) {
1845 /* Wrapped case. */
1846 temp = get_trash_chunk();
1847 memcpy(temp->area, vstart, chunks[1] - vstart);
1848 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1849 vend - chunks[2]);
1850 smp->data.u.str.area = temp->area;
1851 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1852 } else {
1853 /* Contiguous case. */
1854 smp->data.u.str.area = (char *)vstart;
1855 smp->data.u.str.data = vend - vstart;
1856 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1857 }
1858
1859 /* Update context, check wrapping. */
1860 chunks[0] = vend;
1861 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1862 chunks[1] = chunks[3];
1863 chunks[2] = NULL;
1864 }
1865
1866 if (chunks[0] < chunks[1])
1867 smp->flags |= SMP_F_NOT_LAST;
1868
1869 return 1;
1870}
1871
1872/* This function iterates over each parameter of the query string. It uses
1873 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1874 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1875 * An optional parameter name is passed in args[0], otherwise any parameter is
1876 * considered. It supports an optional delimiter argument for the beginning of
1877 * the string in args[1], which defaults to "?".
1878 */
1879static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1880{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001881 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001882 char delim = '?';
1883 const char *name;
1884 int name_len;
1885
Christopher Faulet623af932021-01-29 11:22:15 +01001886 if ((args[0].type && args[0].type != ARGT_STR) ||
Willy Tarreau79e57332018-10-02 16:01:16 +02001887 (args[1].type && args[1].type != ARGT_STR))
1888 return 0;
1889
1890 name = "";
1891 name_len = 0;
1892 if (args->type == ARGT_STR) {
1893 name = args->data.str.area;
1894 name_len = args->data.str.data;
1895 }
1896
1897 if (args[1].type)
1898 delim = *args[1].data.str.area;
1899
1900 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001901 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001902 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001903
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001904 if (!htx)
1905 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001906
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001907 sl = http_get_stline(htx);
1908 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1909 if (!smp->ctx.a[0])
1910 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001911
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001912 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001913
1914 /* Assume that the context is filled with NULL pointer
1915 * before the first call.
1916 * smp->ctx.a[2] = NULL;
1917 * smp->ctx.a[3] = NULL;
1918 */
1919 }
1920
1921 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1922}
1923
1924/* This function iterates over each parameter of the body. This requires
1925 * that the body has been waited for using http-buffer-request. It uses
1926 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001927 * contiguous part of the body, and optionally ctx->a[2..3] to reference the
Willy Tarreau79e57332018-10-02 16:01:16 +02001928 * optional second part if the body wraps at the end of the buffer. An optional
1929 * parameter name is passed in args[0], otherwise any parameter is considered.
1930 */
1931static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1932{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001933 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001934 const char *name;
1935 int name_len;
1936
Christopher Faulet623af932021-01-29 11:22:15 +01001937 if (args[0].type && args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001938 return 0;
1939
1940 name = "";
1941 name_len = 0;
1942 if (args[0].type == ARGT_STR) {
1943 name = args[0].data.str.area;
1944 name_len = args[0].data.str.data;
1945 }
1946
1947 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulete596d182020-05-05 17:46:34 +02001948 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001949 struct buffer *temp;
1950 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001951
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001952 if (!htx)
1953 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001954
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001955 temp = get_trash_chunk();
1956 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1957 struct htx_blk *blk = htx_get_blk(htx, pos);
1958 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001959
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001960 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001961 break;
1962 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001963 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001964 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001965 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001966 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001967
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001968 smp->ctx.a[0] = temp->area;
1969 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001970
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001971 /* Assume that the context is filled with NULL pointer
1972 * before the first call.
1973 * smp->ctx.a[2] = NULL;
1974 * smp->ctx.a[3] = NULL;
1975 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001976
Willy Tarreau79e57332018-10-02 16:01:16 +02001977 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001978
Willy Tarreau79e57332018-10-02 16:01:16 +02001979 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1980}
1981
1982/* Return the signed integer value for the specified url parameter (see url_param
1983 * above).
1984 */
1985static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1986{
1987 int ret = smp_fetch_url_param(args, smp, kw, private);
1988
1989 if (ret > 0) {
1990 smp->data.type = SMP_T_SINT;
1991 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1992 smp->data.u.str.data);
1993 }
1994
1995 return ret;
1996}
1997
1998/* This produces a 32-bit hash of the concatenation of the first occurrence of
1999 * the Host header followed by the path component if it begins with a slash ('/').
2000 * This means that '*' will not be added, resulting in exactly the first Host
2001 * entry. If no Host header is found, then the path is used. The resulting value
2002 * is hashed using the url hash followed by a full avalanche hash and provides a
2003 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
2004 * high-traffic sites without having to store whole paths.
2005 * this differs from the base32 functions in that it includes the url parameters
2006 * as well as the path
2007 */
2008static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
2009{
Christopher Faulet89dc4992019-04-17 12:02:59 +02002010 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02002011 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02002012 struct http_hdr_ctx ctx;
2013 struct htx_sl *sl;
2014 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02002015 unsigned int hash = 0;
Amaury Denoyellec453f952021-07-06 11:40:12 +02002016 struct http_uri_parser parser;
Willy Tarreau79e57332018-10-02 16:01:16 +02002017
Christopher Faulet6d1dd462019-07-15 14:36:03 +02002018 if (!htx)
2019 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002020
Christopher Faulet6d1dd462019-07-15 14:36:03 +02002021 ctx.blk = NULL;
2022 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
2023 /* OK we have the header value in ctx.value */
2024 while (ctx.value.len--)
2025 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02002026 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002027
Christopher Faulet6d1dd462019-07-15 14:36:03 +02002028 /* now retrieve the path */
2029 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02002030 parser = http_uri_parser_init(htx_sl_req_uri(sl));
2031 path = http_parse_path(&parser);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02002032 if (path.len && *(path.ptr) == '/') {
2033 while (path.len--)
2034 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02002035 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02002036
Willy Tarreau79e57332018-10-02 16:01:16 +02002037 hash = full_hash(hash);
2038
2039 smp->data.type = SMP_T_SINT;
2040 smp->data.u.sint = hash;
2041 smp->flags = SMP_F_VOL_1ST;
2042 return 1;
2043}
2044
2045/* This concatenates the source address with the 32-bit hash of the Host and
2046 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
2047 * per-url counters. The result is a binary block from 8 to 20 bytes depending
2048 * on the source address length. The URL hash is stored before the address so
2049 * that in environments where IPv6 is insignificant, truncating the output to
2050 * 8 bytes would still work.
2051 */
2052static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
2053{
Willy Tarreaud68ff012022-05-27 08:57:21 +02002054 const struct sockaddr_storage *src = (smp->strm ? sc_src(smp->strm->scf) : NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +02002055 struct buffer *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02002056
Christopher Faulet6fc817a2021-10-25 07:48:27 +02002057 if (!src)
Willy Tarreau79e57332018-10-02 16:01:16 +02002058 return 0;
2059
2060 if (!smp_fetch_url32(args, smp, kw, private))
2061 return 0;
2062
2063 temp = get_trash_chunk();
2064 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2065 temp->data += sizeof(unsigned int);
2066
Christopher Faulet6fc817a2021-10-25 07:48:27 +02002067 switch (src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02002068 case AF_INET:
2069 memcpy(temp->area + temp->data,
Christopher Faulet6fc817a2021-10-25 07:48:27 +02002070 &((struct sockaddr_in *)src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002071 4);
2072 temp->data += 4;
2073 break;
2074 case AF_INET6:
2075 memcpy(temp->area + temp->data,
Christopher Faulet6fc817a2021-10-25 07:48:27 +02002076 &((struct sockaddr_in6 *)src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002077 16);
2078 temp->data += 16;
2079 break;
2080 default:
2081 return 0;
2082 }
2083
2084 smp->data.u.str = *temp;
2085 smp->data.type = SMP_T_BIN;
2086 return 1;
2087}
2088
2089/************************************************************************/
2090/* Other utility functions */
2091/************************************************************************/
2092
2093/* This function is used to validate the arguments passed to any "hdr" fetch
2094 * keyword. These keywords support an optional positive or negative occurrence
2095 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2096 * is assumed that the types are already the correct ones. Returns 0 on error,
2097 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2098 * error message in case of error, that the caller is responsible for freeing.
2099 * The initial location must either be freeable or NULL.
2100 * Note: this function's pointer is checked from Lua.
2101 */
2102int val_hdr(struct arg *arg, char **err_msg)
2103{
2104 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2105 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2106 return 0;
2107 }
2108 return 1;
2109}
2110
2111/************************************************************************/
2112/* All supported sample fetch keywords must be declared here. */
2113/************************************************************************/
2114
2115/* Note: must not be declared <const> as its list will be overwritten */
2116static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2117 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2118 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2119 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Yves Lafonb4d37082021-02-11 11:01:28 +01002120 { "baseq", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002121
2122 /* capture are allocated and are permanent in the stream */
2123 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2124
2125 /* retrieve these captures from the HTTP logs */
2126 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2127 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2128 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2129
2130 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2131 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2132
2133 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2134 * are only here to match the ACL's name, are request-only and are used
2135 * for ACL compatibility only.
2136 */
2137 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002138 { "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 +02002139 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2140 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2141
2142 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2143 * only here to match the ACL's name, are request-only and are used for
2144 * ACL compatibility only.
2145 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002146 { "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 +02002147 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2148 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2149 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2150
Christopher Fauleta4063562019-08-02 11:51:37 +02002151 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2152 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2153 { "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 +02002154 { "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 +02002155 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2156 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2157 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2158 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2159 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Faulete720c322020-09-02 17:25:18 +02002160 { "pathq", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002161 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2162
2163 /* HTTP protocol on the request path */
2164 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2165 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2166
2167 /* HTTP version on the request path */
2168 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2169 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2170
2171 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2172 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2173 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2174 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2175
2176 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2177 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2178
2179 /* HTTP version on the response path */
2180 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2181 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2182
Christopher Faulete596d182020-05-05 17:46:34 +02002183 { "res.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2184 { "res.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2185 { "res.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2186
2187 { "res.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2188 { "res.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2189
Willy Tarreau79e57332018-10-02 16:01:16 +02002190 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2191 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2192 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2193 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2194
2195 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2196 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2197 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2198 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2199 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2200 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2201 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2202
2203 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2204 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2205 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2206 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2207
2208 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2209 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2210 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2211 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2212 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2213 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2214 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2215
2216 /* scook is valid only on the response and is used for ACL compatibility */
2217 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2218 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2219 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002220
2221 /* shdr is valid only on the response and is used for ACL compatibility */
2222 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2223 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2224 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2225 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2226
2227 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2228 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2229 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2230 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2231 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2232 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2233 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2234 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2235 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2236 { "urlp_val", smp_fetch_url_param_val, ARG2(0,STR,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
Christopher Faulet16032ab2020-04-30 11:30:00 +02002237
Willy Tarreau79e57332018-10-02 16:01:16 +02002238 { /* END */ },
2239}};
2240
Willy Tarreau0108d902018-11-25 19:14:37 +01002241INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002242
2243/*
2244 * Local variables:
2245 * c-indent-level: 8
2246 * c-basic-offset: 8
2247 * End:
2248 */