blob: a66be2031142b9a8f3c8ccde13f2448e1a346541 [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
2 * HTTP samples fetching
3 *
4 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/types.h>
14
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
18
Willy Tarreaub2551052020-06-09 09:07:15 +020019#include <haproxy/api.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020020#include <haproxy/arg.h>
Willy Tarreauac13aea2020-06-04 10:36:03 +020021#include <haproxy/auth.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020022#include <haproxy/base64.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020023#include <haproxy/channel.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020024#include <haproxy/chunk.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020025#include <haproxy/connection.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020026#include <haproxy/global.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020027#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020028#include <haproxy/h1_htx.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020029#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020030#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020031#include <haproxy/http_fetch.h>
Willy Tarreau87735332020-06-04 09:08:41 +020032#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020033#include <haproxy/htx.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020034#include <haproxy/obj_type.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020035#include <haproxy/pool.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020036#include <haproxy/sample.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/stream.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020038#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020039#include <haproxy/version.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020040
Willy Tarreau79e57332018-10-02 16:01:16 +020041
42/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Fauletef453ed2018-10-24 21:39:27 +020043static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
Richard Russo458eafb2019-07-31 11:45:56 -070044/* this is used to convert raw connection buffers to htx */
45static THREAD_LOCAL struct buffer static_raw_htx_chunk;
46static THREAD_LOCAL char *static_raw_htx_buf;
Christopher Fauletef453ed2018-10-24 21:39:27 +020047
Christopher Faulet89dc4992019-04-17 12:02:59 +020048#define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL)
49#define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL)
Willy Tarreau79e57332018-10-02 16:01:16 +020050
Richard Russo458eafb2019-07-31 11:45:56 -070051/* This function returns the static htx chunk, where raw connections get
52 * converted to HTX as needed for samplxsing.
53 */
54struct buffer *get_raw_htx_chunk(void)
55{
56 chunk_reset(&static_raw_htx_chunk);
57 return &static_raw_htx_chunk;
58}
59
60static int alloc_raw_htx_chunk_per_thread()
61{
62 static_raw_htx_buf = malloc(global.tune.bufsize);
63 if (!static_raw_htx_buf)
64 return 0;
65 chunk_init(&static_raw_htx_chunk, static_raw_htx_buf, global.tune.bufsize);
66 return 1;
67}
68
69static void free_raw_htx_chunk_per_thread()
70{
Willy Tarreau61cfdf42021-02-20 10:46:51 +010071 ha_free(&static_raw_htx_buf);
Richard Russo458eafb2019-07-31 11:45:56 -070072}
73
74REGISTER_PER_THREAD_ALLOC(alloc_raw_htx_chunk_per_thread);
75REGISTER_PER_THREAD_FREE(free_raw_htx_chunk_per_thread);
76
Willy Tarreau79e57332018-10-02 16:01:16 +020077/*
78 * Returns the data from Authorization header. Function may be called more
79 * than once so data is stored in txn->auth_data. When no header is found
80 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
81 * searching again for something we are unable to find anyway. However, if
82 * the result if valid, the cache is not reused because we would risk to
83 * have the credentials overwritten by another stream in parallel.
Willy Tarreaueae83722020-04-29 11:52:51 +020084 * The caller is responsible for passing a sample with a valid stream/txn,
85 * and a valid htx.
Willy Tarreau79e57332018-10-02 16:01:16 +020086 */
87
Christopher Fauletcd761952019-07-15 13:58:29 +020088static int get_http_auth(struct sample *smp, struct htx *htx)
Willy Tarreau79e57332018-10-02 16:01:16 +020089{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020090 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020091 struct http_txn *txn = s->txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020092 struct http_hdr_ctx ctx = { .blk = NULL };
93 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020094 struct buffer auth_method;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020095 char *p;
Willy Tarreau79e57332018-10-02 16:01:16 +020096 int len;
97
98#ifdef DEBUG_AUTH
99 printf("Auth for stream %p: %d\n", s, txn->auth.method);
100#endif
Willy Tarreau79e57332018-10-02 16:01:16 +0200101 if (txn->auth.method == HTTP_AUTH_WRONG)
102 return 0;
103
104 txn->auth.method = HTTP_AUTH_WRONG;
105
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200106 if (txn->flags & TX_USE_PX_CONN)
107 hdr = ist("Proxy-Authorization");
108 else
109 hdr = ist("Authorization");
Willy Tarreau79e57332018-10-02 16:01:16 +0200110
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200111 ctx.blk = NULL;
112 if (!http_find_header(htx, hdr, &ctx, 0))
113 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200114
Willy Tarreau17254932020-09-02 07:08:47 +0200115 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
116 if (!p || p == ctx.value.ptr) /* if no space was found or if the space is the first character */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200117 return 0;
Willy Tarreau17254932020-09-02 07:08:47 +0200118 len = p - ctx.value.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +0200119
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200120 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
121 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200122
Remi Tricot-Le Breton2ad2ed42021-10-29 15:25:18 +0200123 /* According to RFC7235, there could be multiple spaces between the
124 * scheme and its value, we must skip all of them.
125 */
126 while (p < istend(ctx.value) && *p == ' ')
127 ++p;
128
129 chunk_initlen(&txn->auth.method_data, p, 0, istend(ctx.value) - p);
Willy Tarreau79e57332018-10-02 16:01:16 +0200130
131 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
132 struct buffer *http_auth = get_trash_chunk();
133
134 len = base64dec(txn->auth.method_data.area,
135 txn->auth.method_data.data,
136 http_auth->area, global.tune.bufsize - 1);
137
138 if (len < 0)
139 return 0;
140
141
142 http_auth->area[len] = '\0';
143
144 p = strchr(http_auth->area, ':');
145
146 if (!p)
147 return 0;
148
149 txn->auth.user = http_auth->area;
150 *p = '\0';
151 txn->auth.pass = p+1;
152
153 txn->auth.method = HTTP_AUTH_BASIC;
154 return 1;
155 }
156
157 return 0;
158}
159
160/* This function ensures that the prerequisites for an L7 fetch are ready,
161 * which means that a request or response is ready. If some data is missing,
162 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200163 * to extract data from L7. If <vol> is non-null during a prefetch, another
164 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200165 *
166 * The function returns :
167 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
168 * decide whether or not an HTTP message is present ;
169 * NULL if the requested data cannot be fetched or if it is certain that
Willy Tarreaueae83722020-04-29 11:52:51 +0200170 * we'll never have any HTTP message there; this includes null strm or chn.
Willy Tarreaua6d98792020-08-12 14:04:52 +0200171 * NULL if the sample's direction does not match the channel's (i.e. the
172 * function was asked to work on the wrong channel)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200173 * The HTX message if ready
174 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200175struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, struct check *check, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200176{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200177 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200178 struct http_txn *txn = NULL;
179 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200180 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100181 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200182
Willy Tarreaua6d98792020-08-12 14:04:52 +0200183 if (chn &&
184 (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ && (chn->flags & CF_ISRESP)) ||
185 ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES && !(chn->flags & CF_ISRESP))))
186 return 0;
187
Christopher Fauletef453ed2018-10-24 21:39:27 +0200188 /* Note: it is possible that <s> is NULL when called before stream
189 * initialization (eg: tcp-request connection), so this function is the
190 * one responsible for guarding against this case for all HTTP users.
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200191 *
192 * In the health check context, the stream and the channel must be NULL
193 * and <check> must be set. In this case, only the input buffer,
194 * corresponding to the response, is considered. It is the caller
195 * responsibility to provide <check>.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200196 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200197 BUG_ON(check && (s || chn));
198 if (!s || !chn) {
199 if (check) {
200 htx = htxbuf(&check->bi);
201
202 /* Analyse not yet started */
203 if (htx_is_empty(htx) || htx->first == -1)
204 return NULL;
205
206 sl = http_get_stline(htx);
207 if (vol && !sl) {
208 /* The start-line was already forwarded, it is too late to fetch anything */
209 return NULL;
210 }
211 goto end;
212 }
213
Christopher Fauletef453ed2018-10-24 21:39:27 +0200214 return NULL;
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200215 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200216
Christopher Faulet75f619a2021-03-08 19:12:58 +0100217 if (!s->txn && !http_create_txn(s))
218 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200219 txn = s->txn;
220 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200221
Christopher Fauleteca88542019-04-03 10:12:42 +0200222 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200223 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200224
Christopher Faulet89dc4992019-04-17 12:02:59 +0200225 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
226 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200227
Christopher Faulet89dc4992019-04-17 12:02:59 +0200228 if (msg->msg_state < HTTP_MSG_BODY) {
229 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200230 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200231 /* Parsing is done by the mux, just wait */
232 smp->flags |= SMP_F_MAY_CHANGE;
233 return NULL;
234 }
235 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200236 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200237 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200238 /* The start-line was already forwarded, it is too late to fetch anything */
239 return NULL;
240 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200241 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200242 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200243 struct buffer *buf;
244 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200245 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200246 union h1_sl h1sl;
247 unsigned int flags = HTX_FL_NONE;
248 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200249
Christopher Faulet89dc4992019-04-17 12:02:59 +0200250 /* no HTTP fetch on the response in TCP mode */
251 if (chn->flags & CF_ISRESP)
252 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200253
Christopher Faulet89dc4992019-04-17 12:02:59 +0200254 /* Now we are working on the request only */
255 buf = &chn->buf;
256 if (b_head(buf) + b_data(buf) > b_wrap(buf))
257 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200258
Christopher Faulet89dc4992019-04-17 12:02:59 +0200259 h1m_init_req(&h1m);
260 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
261 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
262 if (ret <= 0) {
263 /* Invalid or too big*/
264 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200265 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100266
Christopher Faulet89dc4992019-04-17 12:02:59 +0200267 /* wait for a full request */
268 smp->flags |= SMP_F_MAY_CHANGE;
269 return NULL;
270 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100271
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500272 /* OK we just got a valid HTTP message. We have to convert it
Christopher Faulet89dc4992019-04-17 12:02:59 +0200273 * into an HTX message.
274 */
275 if (unlikely(h1sl.rq.v.len == 0)) {
276 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
277 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200278 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200279 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200280 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200281
282 /* Set HTX start-line flags */
283 if (h1m.flags & H1_MF_VER_11)
284 flags |= HTX_SL_F_VER_11;
285 if (h1m.flags & H1_MF_XFER_ENC)
286 flags |= HTX_SL_F_XFER_ENC;
287 flags |= HTX_SL_F_XFER_LEN;
288 if (h1m.flags & H1_MF_CHNK)
289 flags |= HTX_SL_F_CHNK;
290 else if (h1m.flags & H1_MF_CLEN)
291 flags |= HTX_SL_F_CLEN;
292
Richard Russo458eafb2019-07-31 11:45:56 -0700293 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200294 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
295 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200296 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200297 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200298 }
299
300 /* OK we just got a valid HTTP message. If not already done by
301 * HTTP analyzers, we have some minor preparation to perform so
302 * that further checks can rely on HTTP tests.
303 */
304 if (sl && msg->msg_state < HTTP_MSG_BODY) {
305 if (!(chn->flags & CF_ISRESP)) {
306 txn->meth = sl->info.req.meth;
307 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
308 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200309 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200310 else
311 txn->status = sl->info.res.status;
312 if (sl->flags & HTX_SL_F_VER_11)
313 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200314 }
315
316 /* everything's OK */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200317 end:
Christopher Fauletef453ed2018-10-24 21:39:27 +0200318 return htx;
319}
320
Willy Tarreau79e57332018-10-02 16:01:16 +0200321/* This function fetches the method of current HTTP request and stores
322 * it in the global pattern struct as a chunk. There are two possibilities :
323 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
324 * in <len> and <ptr> is NULL ;
325 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
326 * <len> to its length.
327 * This is intended to be used with pat_match_meth() only.
328 */
329static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
330{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200331 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200332 struct http_txn *txn;
Christopher Faulet5b1cf0c2022-06-22 17:16:41 +0200333 struct htx *htx;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200334 int meth;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200335
Christopher Faulete073a552022-07-06 17:53:02 +0200336 txn = (smp->strm ? smp->strm->txn : NULL);
Willy Tarreaua6d98792020-08-12 14:04:52 +0200337 if (!txn)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200338 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200339
Willy Tarreaubf684b32022-07-10 13:13:52 +0200340 meth = txn->meth;
341 if (meth == HTTP_METH_OTHER) {
Christopher Faulet5b1cf0c2022-06-22 17:16:41 +0200342 htx = smp_prefetch_htx(smp, chn, NULL, 1);
343 if (!htx)
344 return 0;
345 }
346
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200347 smp->data.type = SMP_T_METH;
348 smp->data.u.meth.meth = meth;
349 if (meth == HTTP_METH_OTHER) {
350 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200351
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200352 sl = http_get_stline(htx);
353 smp->flags |= SMP_F_CONST;
354 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
355 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200356 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200357 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200358 return 1;
359}
360
361static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
362{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200363 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200364 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200365 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200366 char *ptr;
367 int len;
368
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200369 if (!htx)
370 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200371
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200372 sl = http_get_stline(htx);
373 len = HTX_SL_REQ_VLEN(sl);
374 ptr = HTX_SL_REQ_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200375
376 while ((len-- > 0) && (*ptr++ != '/'));
377 if (len <= 0)
378 return 0;
379
380 smp->data.type = SMP_T_STR;
381 smp->data.u.str.area = ptr;
382 smp->data.u.str.data = len;
383
384 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
385 return 1;
386}
387
388static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
389{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200390 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200391 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200392 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200393 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200394 char *ptr;
395 int len;
396
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200397 if (!htx)
398 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200399
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200400 sl = http_get_stline(htx);
401 len = HTX_SL_RES_VLEN(sl);
402 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200403
404 while ((len-- > 0) && (*ptr++ != '/'));
405 if (len <= 0)
406 return 0;
407
408 smp->data.type = SMP_T_STR;
409 smp->data.u.str.area = ptr;
410 smp->data.u.str.data = len;
411
412 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
413 return 1;
414}
415
416/* 3. Check on Status Code. We manipulate integers here. */
417static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
418{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200419 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200420 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200421 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200422 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200423 char *ptr;
424 int len;
425
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200426 if (!htx)
427 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200428
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200429 sl = http_get_stline(htx);
430 len = HTX_SL_RES_CLEN(sl);
431 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200432
433 smp->data.type = SMP_T_SINT;
434 smp->data.u.sint = __strl2ui(ptr, len);
435 smp->flags = SMP_F_VOL_1ST;
436 return 1;
437}
438
439static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
440{
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100441 struct ist unique_id;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100442
Willy Tarreau79e57332018-10-02 16:01:16 +0200443 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
444 return 0;
445
Willy Tarreaua1062a42020-04-29 11:50:38 +0200446 if (!smp->strm)
447 return 0;
448
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100449 unique_id = stream_generate_unique_id(smp->strm, &smp->sess->fe->format_unique_id);
450 if (!isttest(unique_id))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100451 return 0;
452
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100453 smp->data.u.str.area = smp->strm->unique_id.ptr;
454 smp->data.u.str.data = smp->strm->unique_id.len;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100455 smp->data.type = SMP_T_STR;
Willy Tarreau79e57332018-10-02 16:01:16 +0200456 smp->flags = SMP_F_CONST;
457 return 1;
458}
459
460/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800461 * empty line which separes headers from the body. This is useful
462 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200463 */
464static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
465{
Christopher Faulete596d182020-05-05 17:46:34 +0200466 /* possible keywords: req.hdrs, res.hdrs */
467 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200468 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200469 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200470 struct buffer *temp;
471 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200472
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200473 if (!htx)
474 return 0;
475 temp = get_trash_chunk();
476 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
477 struct htx_blk *blk = htx_get_blk(htx, pos);
478 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200479
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200480 if (type == HTX_BLK_HDR) {
481 struct ist n = htx_get_blk_name(htx, blk);
482 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200483
Christopher Faulet53a899b2019-10-08 16:38:42 +0200484 if (!h1_format_htx_hdr(n, v, temp))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200485 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200486 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200487 else if (type == HTX_BLK_EOH) {
488 if (!chunk_memcat(temp, "\r\n", 2))
489 return 0;
490 break;
491 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200492 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200493 smp->data.type = SMP_T_STR;
494 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200495 return 1;
496}
497
498/* Returns the header request in a length/value encoded format.
499 * This is useful for exchanges with the SPOE.
500 *
501 * A "length value" is a multibyte code encoding numbers. It uses the
502 * SPOE format. The encoding is the following:
503 *
504 * Each couple "header name" / "header value" is composed
505 * like this:
506 * "length value" "header name bytes"
507 * "length value" "header value bytes"
508 * When the last header is reached, the header name and the header
509 * value are empty. Their length are 0
510 */
511static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
512{
Christopher Faulete596d182020-05-05 17:46:34 +0200513 /* possible keywords: req.hdrs_bin, res.hdrs_bin */
514 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200515 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200516 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200517 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200518 char *p, *end;
519 int32_t pos;
520 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200521
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200522 if (!htx)
523 return 0;
524 temp = get_trash_chunk();
525 p = temp->area;
526 end = temp->area + temp->size;
527 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
528 struct htx_blk *blk = htx_get_blk(htx, pos);
529 enum htx_blk_type type = htx_get_blk_type(blk);
530 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200531
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200532 if (type == HTX_BLK_HDR) {
533 n = htx_get_blk_name(htx,blk);
534 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200535
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200536 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200537 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200538 if (ret == -1)
539 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200540 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200541 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200542 memcpy(p, n.ptr, n.len);
543 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200544
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200545 /* encode the header value. */
546 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200547 if (ret == -1)
548 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200549 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200550 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200551 memcpy(p, v.ptr, v.len);
552 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200553
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200554 }
555 else if (type == HTX_BLK_EOH) {
556 /* encode the end of the header list with empty
557 * header name and header value.
558 */
559 ret = encode_varint(0, &p, end);
560 if (ret == -1)
561 return 0;
562 ret = encode_varint(0, &p, end);
563 if (ret == -1)
564 return 0;
565 break;
566 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200567 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200568
569 /* Initialise sample data which will be filled. */
570 smp->data.type = SMP_T_BIN;
571 smp->data.u.str.area = temp->area;
572 smp->data.u.str.data = p - temp->area;
573 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200574 return 1;
575}
576
577/* returns the longest available part of the body. This requires that the body
578 * has been waited for using http-buffer-request.
579 */
580static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
581{
Christopher Faulete596d182020-05-05 17:46:34 +0200582 /* possible keywords: req.body, res.body */
583 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200584 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200585 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200586 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200587 int32_t pos;
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100588 int finished = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200589
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200590 if (!htx)
591 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200592
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200593 temp = get_trash_chunk();
594 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
595 struct htx_blk *blk = htx_get_blk(htx, pos);
596 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200597
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100598 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) {
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100599 finished = 1;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200600 break;
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100601 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200602 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +0200603 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200604 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200605 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200606 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200607
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200608 smp->data.type = SMP_T_BIN;
609 smp->data.u.str = *temp;
610 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200611
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100612 if (!finished && (check || (chn && !channel_full(chn, global.tune.maxrewrite) &&
613 !(chn->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)))))
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200614 smp->flags |= SMP_F_MAY_CHANGE;
615
Willy Tarreau79e57332018-10-02 16:01:16 +0200616 return 1;
617}
618
619
620/* returns the available length of the body. This requires that the body
621 * has been waited for using http-buffer-request.
622 */
623static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
624{
Christopher Faulete596d182020-05-05 17:46:34 +0200625 /* possible keywords: req.body_len, res.body_len */
626 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200627 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200628 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200629 int32_t pos;
630 unsigned long long len = 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100631
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200632 if (!htx)
633 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100634
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200635 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
636 struct htx_blk *blk = htx_get_blk(htx, pos);
637 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100638
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100639 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200640 break;
641 if (type == HTX_BLK_DATA)
642 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200643 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200644
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200645 smp->data.type = SMP_T_SINT;
646 smp->data.u.sint = len;
647 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200648 return 1;
649}
650
651
652/* returns the advertised length of the body, or the advertised size of the
653 * chunks available in the buffer. This requires that the body has been waited
654 * for using http-buffer-request.
655 */
656static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
657{
Christopher Faulete596d182020-05-05 17:46:34 +0200658 /* possible keywords: req.body_size, res.body_size */
659 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200660 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200661 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200662 int32_t pos;
663 unsigned long long len = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200664
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200665 if (!htx)
666 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100667
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200668 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
669 struct htx_blk *blk = htx_get_blk(htx, pos);
670 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100671
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100672 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200673 break;
674 if (type == HTX_BLK_DATA)
675 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200676 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200677 if (htx->extra != ULLONG_MAX)
678 len += htx->extra;
Willy Tarreau79e57332018-10-02 16:01:16 +0200679
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200680 smp->data.type = SMP_T_SINT;
681 smp->data.u.sint = len;
682 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200683 return 1;
684}
685
686
687/* 4. Check on URL/URI. A pointer to the URI is stored. */
688static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
689{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200690 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200691 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200692 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200693
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200694 if (!htx)
695 return 0;
696 sl = http_get_stline(htx);
697 smp->data.type = SMP_T_STR;
698 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
699 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
700 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200701 return 1;
702}
703
704static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
705{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200706 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200707 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200708 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200709 struct sockaddr_storage addr;
710
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200711 memset(&addr, 0, sizeof(addr));
712
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200713 if (!htx)
714 return 0;
715 sl = http_get_stline(htx);
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200716 if (url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL) < 0)
717 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200718
Willy Tarreau48584642021-05-09 10:32:54 +0200719 if (addr.ss_family != AF_INET)
Willy Tarreau79e57332018-10-02 16:01:16 +0200720 return 0;
721
722 smp->data.type = SMP_T_IPV4;
723 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
724 smp->flags = 0;
725 return 1;
726}
727
728static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
729{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200730 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200731 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200732 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200733 struct sockaddr_storage addr;
734
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200735 memset(&addr, 0, sizeof(addr));
736
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200737 if (!htx)
738 return 0;
739 sl = http_get_stline(htx);
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200740 if (url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL) < 0)
741 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200742
Willy Tarreau48584642021-05-09 10:32:54 +0200743 if (addr.ss_family != AF_INET)
Willy Tarreau79e57332018-10-02 16:01:16 +0200744 return 0;
745
746 smp->data.type = SMP_T_SINT;
Willy Tarreau48584642021-05-09 10:32:54 +0200747 smp->data.u.sint = get_host_port(&addr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200748 smp->flags = 0;
749 return 1;
750}
751
752/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
753 * Accepts an optional argument of type string containing the header field name,
754 * and an optional argument of type signed or unsigned integer to request an
755 * explicit occurrence of the header. Note that in the event of a missing name,
756 * headers are considered from the first one. It does not stop on commas and
757 * returns full lines instead (useful for User-Agent or Date for example).
758 */
759static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
760{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200761 /* possible keywords: req.fhdr, res.fhdr */
762 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200763 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200764 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200765 struct http_hdr_ctx *ctx = smp->ctx.a[0];
766 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200767 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200768
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200769 if (!ctx) {
770 /* first call */
771 ctx = &static_http_hdr_ctx;
772 ctx->blk = NULL;
773 smp->ctx.a[0] = ctx;
774 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200775
Christopher Faulet623af932021-01-29 11:22:15 +0100776 if (args[0].type != ARGT_STR)
777 return 0;
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100778 name = ist2(args[0].data.str.area, args[0].data.str.data);
Willy Tarreau79e57332018-10-02 16:01:16 +0200779
Christopher Faulet623af932021-01-29 11:22:15 +0100780 if (args[1].type == ARGT_SINT)
781 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200782
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200783 if (!htx)
784 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200785
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200786 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
787 /* search for header from the beginning */
788 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200789
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200790 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
791 /* no explicit occurrence and single fetch => last header by default */
792 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200793
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200794 if (!occ)
795 /* prepare to report multiple occurrences for ACL fetches */
796 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200797
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200798 smp->data.type = SMP_T_STR;
799 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
800 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
801 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200802 smp->flags &= ~SMP_F_NOT_LAST;
803 return 0;
804}
805
806/* 6. Check on HTTP header count. The number of occurrences is returned.
807 * Accepts exactly 1 argument of type string. It does not stop on commas and
808 * returns full lines instead (useful for User-Agent or Date for example).
809 */
810static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
811{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200812 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
813 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200814 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200815 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200816 struct http_hdr_ctx ctx;
817 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200818 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200819
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200820 if (!htx)
821 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200822
Christopher Faulet623af932021-01-29 11:22:15 +0100823 if (args->type == ARGT_STR) {
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100824 name = ist2(args->data.str.area, args->data.str.data);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200825 } else {
Tim Duesterhus68a088d2021-02-28 16:11:37 +0100826 name = IST_NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200827 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200828
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200829 ctx.blk = NULL;
830 cnt = 0;
831 while (http_find_header(htx, name, &ctx, 1))
832 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200833 smp->data.type = SMP_T_SINT;
834 smp->data.u.sint = cnt;
835 smp->flags = SMP_F_VOL_HDR;
836 return 1;
837}
838
839static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
840{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200841 /* possible keywords: req.hdr_names, res.hdr_names */
842 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200843 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200844 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200845 struct buffer *temp;
846 char del = ',';
847
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200848 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200849
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200850 if (!htx)
851 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200852
Christopher Faulet623af932021-01-29 11:22:15 +0100853 if (args->type == ARGT_STR)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200854 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200855
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200856 temp = get_trash_chunk();
857 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
858 struct htx_blk *blk = htx_get_blk(htx, pos);
859 enum htx_blk_type type = htx_get_blk_type(blk);
860 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200861
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200862 if (type == HTX_BLK_EOH)
863 break;
864 if (type != HTX_BLK_HDR)
865 continue;
866 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200867
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200868 if (temp->data)
869 temp->area[temp->data++] = del;
870 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200871 }
872
873 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200874 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200875 smp->flags = SMP_F_VOL_HDR;
876 return 1;
877}
878
879/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
880 * Accepts an optional argument of type string containing the header field name,
881 * and an optional argument of type signed or unsigned integer to request an
882 * explicit occurrence of the header. Note that in the event of a missing name,
883 * headers are considered from the first one.
884 */
885static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
886{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200887 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
888 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200889 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200890 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200891 struct http_hdr_ctx *ctx = smp->ctx.a[0];
892 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200893 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200894
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200895 if (!ctx) {
896 /* first call */
897 ctx = &static_http_hdr_ctx;
898 ctx->blk = NULL;
899 smp->ctx.a[0] = ctx;
900 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200901
Christopher Faulet623af932021-01-29 11:22:15 +0100902 if (args[0].type != ARGT_STR)
903 return 0;
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100904 name = ist2(args[0].data.str.area, args[0].data.str.data);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200905
Christopher Faulet623af932021-01-29 11:22:15 +0100906 if (args[1].type == ARGT_SINT)
907 occ = args[1].data.sint;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200908
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200909 if (!htx)
910 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200911
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200912 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
913 /* search for header from the beginning */
914 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200915
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200916 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
917 /* no explicit occurrence and single fetch => last header by default */
918 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200919
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200920 if (!occ)
921 /* prepare to report multiple occurrences for ACL fetches */
922 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200923
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200924 smp->data.type = SMP_T_STR;
925 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
926 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
927 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200928
929 smp->flags &= ~SMP_F_NOT_LAST;
930 return 0;
931}
932
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200933/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
934 * the right channel. So instead of duplicating the code, we just change the
935 * keyword and then fallback on smp_fetch_hdr().
936 */
937static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
938{
939 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
940 return smp_fetch_hdr(args, smp, kw, private);
941}
942
Willy Tarreau79e57332018-10-02 16:01:16 +0200943/* 6. Check on HTTP header count. The number of occurrences is returned.
944 * Accepts exactly 1 argument of type string.
945 */
946static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
947{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200948 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
949 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200950 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200951 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200952 struct http_hdr_ctx ctx;
953 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200954 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200955
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200956 if (!htx)
957 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200958
Christopher Faulet623af932021-01-29 11:22:15 +0100959 if (args->type == ARGT_STR) {
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100960 name = ist2(args->data.str.area, args->data.str.data);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200961 } else {
Tim Duesterhus68a088d2021-02-28 16:11:37 +0100962 name = IST_NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200963 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200964
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200965 ctx.blk = NULL;
966 cnt = 0;
967 while (http_find_header(htx, name, &ctx, 0))
968 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200969
970 smp->data.type = SMP_T_SINT;
971 smp->data.u.sint = cnt;
972 smp->flags = SMP_F_VOL_HDR;
973 return 1;
974}
975
976/* Fetch an HTTP header's integer value. The integer value is returned. It
977 * takes a mandatory argument of type string and an optional one of type int
978 * to designate a specific occurrence. It returns an unsigned integer, which
979 * may or may not be appropriate for everything.
980 */
981static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
982{
983 int ret = smp_fetch_hdr(args, smp, kw, private);
984
985 if (ret > 0) {
986 smp->data.type = SMP_T_SINT;
987 smp->data.u.sint = strl2ic(smp->data.u.str.area,
988 smp->data.u.str.data);
989 }
990
991 return ret;
992}
993
994/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
995 * and an optional one of type int to designate a specific occurrence.
Willy Tarreau7b0e00d2021-03-25 14:12:29 +0100996 * It returns an IPv4 or IPv6 address. Addresses surrounded by invalid chars
997 * are rejected. However IPv4 addresses may be followed with a colon and a
998 * valid port number.
Willy Tarreau79e57332018-10-02 16:01:16 +0200999 */
1000static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1001{
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001002 struct buffer *temp = get_trash_chunk();
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001003 int ret, len;
1004 int port;
Willy Tarreau79e57332018-10-02 16:01:16 +02001005
1006 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001007 if (smp->data.u.str.data < temp->size - 1) {
1008 memcpy(temp->area, smp->data.u.str.area,
1009 smp->data.u.str.data);
1010 temp->area[smp->data.u.str.data] = '\0';
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001011 len = url2ipv4((char *) temp->area, &smp->data.u.ipv4);
Willy Tarreau645dc082021-03-31 11:41:36 +02001012 if (len > 0 && len == smp->data.u.str.data) {
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001013 /* plain IPv4 address */
1014 smp->data.type = SMP_T_IPV4;
1015 break;
1016 } else if (len > 0 && temp->area[len] == ':' &&
1017 strl2irc(temp->area + len + 1, smp->data.u.str.data - len - 1, &port) == 0 &&
1018 port >= 0 && port <= 65535) {
1019 /* IPv4 address suffixed with ':' followed by a valid port number */
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001020 smp->data.type = SMP_T_IPV4;
1021 break;
1022 } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1023 smp->data.type = SMP_T_IPV6;
1024 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001025 }
1026 }
1027
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001028 /* if the header doesn't match an IP address, fetch next one */
1029 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001030 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001031 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001032 return ret;
1033}
Willy Tarreau79e57332018-10-02 16:01:16 +02001034
Christopher Faulete720c322020-09-02 17:25:18 +02001035/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at the
1036 * first '/' after the possible hostname. It ends before the possible '?' except
1037 * for 'pathq' keyword.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001038 */
1039static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1040{
1041 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001042 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001043 struct htx_sl *sl;
1044 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001045
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001046 if (!htx)
1047 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001048
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001049 sl = http_get_stline(htx);
Christopher Faulete720c322020-09-02 17:25:18 +02001050 path = http_get_path(htx_sl_req_uri(sl));
1051
Yves Lafonb4d37082021-02-11 11:01:28 +01001052 if (kw[4] == 'q' && (kw[0] == 'p' || kw[0] == 'b')) // pathq or baseq
Christopher Faulete720c322020-09-02 17:25:18 +02001053 path = http_get_path(htx_sl_req_uri(sl));
1054 else
1055 path = iststop(http_get_path(htx_sl_req_uri(sl)), '?');
1056
Tim Duesterhused526372020-03-05 17:56:33 +01001057 if (!isttest(path))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001058 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001059
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001060 /* OK, we got the '/' ! */
1061 smp->data.type = SMP_T_STR;
1062 smp->data.u.str.area = path.ptr;
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001063 smp->data.u.str.data = path.len;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001064 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001065 return 1;
1066}
1067
1068/* This produces a concatenation of the first occurrence of the Host header
1069 * followed by the path component if it begins with a slash ('/'). This means
1070 * that '*' will not be added, resulting in exactly the first Host entry.
1071 * If no Host header is found, then the path is returned as-is. The returned
1072 * value is stored in the trash so it does not need to be marked constant.
1073 * The returned sample is of type string.
1074 */
1075static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1076{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001077 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001078 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001079 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001080 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001081 struct http_hdr_ctx ctx;
1082 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001083
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001084 if (!htx)
1085 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001086
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001087 ctx.blk = NULL;
1088 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1089 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001090
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001091 /* OK we have the header value in ctx.value */
1092 temp = get_trash_chunk();
1093 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001094
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001095 /* now retrieve the path */
1096 sl = http_get_stline(htx);
1097 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001098 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001099 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001100
Yves Lafonb4d37082021-02-11 11:01:28 +01001101 if (kw[4] == 'q' && kw[0] == 'b') { // baseq
1102 len = path.len;
1103 } else {
1104 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1105 ;
1106 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001107
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001108 if (len && *(path.ptr) == '/')
1109 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001110 }
1111
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001112 smp->data.type = SMP_T_STR;
1113 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001114 smp->flags = SMP_F_VOL_1ST;
1115 return 1;
1116}
1117
1118/* This produces a 32-bit hash of the concatenation of the first occurrence of
1119 * the Host header followed by the path component if it begins with a slash ('/').
1120 * This means that '*' will not be added, resulting in exactly the first Host
1121 * entry. If no Host header is found, then the path is used. The resulting value
1122 * is hashed using the path hash followed by a full avalanche hash and provides a
1123 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1124 * high-traffic sites without having to store whole paths.
1125 */
1126static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1127{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001128 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001129 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001130 struct htx_sl *sl;
1131 struct http_hdr_ctx ctx;
1132 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001133 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001134
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001135 if (!htx)
1136 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001137
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001138 ctx.blk = NULL;
1139 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1140 /* OK we have the header value in ctx.value */
1141 while (ctx.value.len--)
1142 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001143 }
1144
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001145 /* now retrieve the path */
1146 sl = http_get_stline(htx);
1147 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001148 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001149 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001150
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001151 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1152 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001153
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001154 if (len && *(path.ptr) == '/') {
1155 while (len--)
1156 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001157 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001158 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001159
Willy Tarreau79e57332018-10-02 16:01:16 +02001160 hash = full_hash(hash);
1161
1162 smp->data.type = SMP_T_SINT;
1163 smp->data.u.sint = hash;
1164 smp->flags = SMP_F_VOL_1ST;
1165 return 1;
1166}
1167
1168/* This concatenates the source address with the 32-bit hash of the Host and
1169 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1170 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1171 * on the source address length. The path hash is stored before the address so
1172 * that in environments where IPv6 is insignificant, truncating the output to
1173 * 8 bytes would still work.
1174 */
1175static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1176{
1177 struct buffer *temp;
1178 struct connection *cli_conn = objt_conn(smp->sess->origin);
1179
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001180 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001181 return 0;
1182
1183 if (!smp_fetch_base32(args, smp, kw, private))
1184 return 0;
1185
1186 temp = get_trash_chunk();
1187 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1188 temp->data += sizeof(unsigned int);
1189
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001190 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001191 case AF_INET:
1192 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001193 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001194 4);
1195 temp->data += 4;
1196 break;
1197 case AF_INET6:
1198 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001199 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001200 16);
1201 temp->data += 16;
1202 break;
1203 default:
1204 return 0;
1205 }
1206
1207 smp->data.u.str = *temp;
1208 smp->data.type = SMP_T_BIN;
1209 return 1;
1210}
1211
1212/* Extracts the query string, which comes after the question mark '?'. If no
1213 * question mark is found, nothing is returned. Otherwise it returns a sample
1214 * of type string carrying the whole query string.
1215 */
1216static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1217{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001218 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001219 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001220 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001221 char *ptr, *end;
1222
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001223 if (!htx)
1224 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001225
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001226 sl = http_get_stline(htx);
1227 ptr = HTX_SL_REQ_UPTR(sl);
1228 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001229
1230 /* look up the '?' */
1231 do {
1232 if (ptr == end)
1233 return 0;
1234 } while (*ptr++ != '?');
1235
1236 smp->data.type = SMP_T_STR;
1237 smp->data.u.str.area = ptr;
1238 smp->data.u.str.data = end - ptr;
1239 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1240 return 1;
1241}
1242
1243static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1244{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001245 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001246 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001247
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001248 if (!htx)
1249 return 0;
1250 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001251 smp->data.u.sint = 1;
1252 return 1;
1253}
1254
1255/* return a valid test if the current request is the first one on the connection */
1256static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1257{
Willy Tarreau79512b62020-04-29 11:52:13 +02001258 if (!smp->strm)
1259 return 0;
1260
Willy Tarreau79e57332018-10-02 16:01:16 +02001261 smp->data.type = SMP_T_BOOL;
1262 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1263 return 1;
1264}
1265
Christopher Fauleta4063562019-08-02 11:51:37 +02001266/* Fetch the authentication method if there is an Authorization header. It
1267 * relies on get_http_auth()
1268 */
1269static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1270{
1271 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001272 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001273 struct http_txn *txn;
1274
1275 if (!htx)
1276 return 0;
1277
1278 txn = smp->strm->txn;
1279 if (!get_http_auth(smp, htx))
1280 return 0;
1281
1282 switch (txn->auth.method) {
1283 case HTTP_AUTH_BASIC:
1284 smp->data.u.str.area = "Basic";
1285 smp->data.u.str.data = 5;
1286 break;
1287 case HTTP_AUTH_DIGEST:
1288 /* Unexpected because not supported */
1289 smp->data.u.str.area = "Digest";
1290 smp->data.u.str.data = 6;
1291 break;
1292 default:
1293 return 0;
1294 }
1295
1296 smp->data.type = SMP_T_STR;
1297 smp->flags = SMP_F_CONST;
1298 return 1;
1299}
1300
1301/* Fetch the user supplied if there is an Authorization header. It relies on
1302 * get_http_auth()
1303 */
1304static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1305{
1306 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001307 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001308 struct http_txn *txn;
1309
1310 if (!htx)
1311 return 0;
1312
1313 txn = smp->strm->txn;
1314 if (!get_http_auth(smp, htx))
1315 return 0;
1316
1317 smp->data.type = SMP_T_STR;
1318 smp->data.u.str.area = txn->auth.user;
1319 smp->data.u.str.data = strlen(txn->auth.user);
1320 smp->flags = SMP_F_CONST;
1321 return 1;
1322}
1323
1324/* Fetch the password supplied if there is an Authorization header. It relies on
1325 * get_http_auth()
1326 */
1327static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1328{
1329 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001330 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001331 struct http_txn *txn;
1332
1333 if (!htx)
1334 return 0;
1335
1336 txn = smp->strm->txn;
1337 if (!get_http_auth(smp, htx))
1338 return 0;
1339
1340 smp->data.type = SMP_T_STR;
1341 smp->data.u.str.area = txn->auth.pass;
1342 smp->data.u.str.data = strlen(txn->auth.pass);
1343 smp->flags = SMP_F_CONST;
1344 return 1;
1345}
1346
Willy Tarreau79e57332018-10-02 16:01:16 +02001347/* Accepts exactly 1 argument of type userlist */
1348static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1349{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001350 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001351 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001352
Christopher Faulet623af932021-01-29 11:22:15 +01001353 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001354 return 0;
1355
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001356 if (!htx)
1357 return 0;
1358 if (!get_http_auth(smp, htx))
1359 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001360
1361 smp->data.type = SMP_T_BOOL;
1362 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001363 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001364 return 1;
1365}
1366
1367/* Accepts exactly 1 argument of type userlist */
1368static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1369{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001370 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001371 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001372
Christopher Faulet623af932021-01-29 11:22:15 +01001373 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001374 return 0;
1375
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001376 if (!htx)
1377 return 0;
1378 if (!get_http_auth(smp, htx))
1379 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001380
Willy Tarreau79e57332018-10-02 16:01:16 +02001381 /* if the user does not belong to the userlist or has a wrong password,
1382 * report that it unconditionally does not match. Otherwise we return
1383 * a string containing the username.
1384 */
1385 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1386 smp->strm->txn->auth.pass))
1387 return 0;
1388
1389 /* pat_match_auth() will need the user list */
1390 smp->ctx.a[0] = args->data.usr;
1391
1392 smp->data.type = SMP_T_STR;
1393 smp->flags = SMP_F_CONST;
1394 smp->data.u.str.area = smp->strm->txn->auth.user;
1395 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1396
1397 return 1;
1398}
1399
1400/* Fetch a captured HTTP request header. The index is the position of
1401 * the "capture" option in the configuration file
1402 */
1403static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1404{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001405 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001406 int idx;
1407
Christopher Faulet623af932021-01-29 11:22:15 +01001408 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001409 return 0;
1410
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001411 if (!smp->strm)
1412 return 0;
1413
1414 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001415 idx = args->data.sint;
1416
1417 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1418 return 0;
1419
1420 smp->data.type = SMP_T_STR;
1421 smp->flags |= SMP_F_CONST;
1422 smp->data.u.str.area = smp->strm->req_cap[idx];
1423 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1424
1425 return 1;
1426}
1427
1428/* Fetch a captured HTTP response header. The index is the position of
1429 * the "capture" option in the configuration file
1430 */
1431static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1432{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001433 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001434 int idx;
1435
Christopher Faulet623af932021-01-29 11:22:15 +01001436 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001437 return 0;
1438
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001439 if (!smp->strm)
1440 return 0;
1441
1442 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001443 idx = args->data.sint;
1444
1445 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1446 return 0;
1447
1448 smp->data.type = SMP_T_STR;
1449 smp->flags |= SMP_F_CONST;
1450 smp->data.u.str.area = smp->strm->res_cap[idx];
1451 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1452
1453 return 1;
1454}
1455
1456/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1457static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1458{
1459 struct buffer *temp;
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001460 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001461 char *ptr;
1462
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001463 if (!smp->strm)
1464 return 0;
1465
1466 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001467 if (!txn || !txn->uri)
1468 return 0;
1469
1470 ptr = txn->uri;
1471
1472 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1473 ptr++;
1474
1475 temp = get_trash_chunk();
1476 temp->area = txn->uri;
1477 temp->data = ptr - txn->uri;
1478 smp->data.u.str = *temp;
1479 smp->data.type = SMP_T_STR;
1480 smp->flags = SMP_F_CONST;
1481
1482 return 1;
1483
1484}
1485
1486/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1487static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1488{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001489 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001490 struct ist path;
1491 const char *ptr;
1492
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001493 if (!smp->strm)
1494 return 0;
1495
1496 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001497 if (!txn || !txn->uri)
1498 return 0;
1499
1500 ptr = txn->uri;
1501
1502 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1503 ptr++;
1504
1505 if (!*ptr)
1506 return 0;
1507
Christopher Faulet78337bb2018-11-15 14:35:18 +01001508 /* skip the first space and find space after URI */
1509 path = ist2(++ptr, 0);
1510 while (*ptr != ' ' && *ptr != '\0')
1511 ptr++;
1512 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001513
Christopher Faulet78337bb2018-11-15 14:35:18 +01001514 path = http_get_path(path);
Tim Duesterhused526372020-03-05 17:56:33 +01001515 if (!isttest(path))
Willy Tarreau79e57332018-10-02 16:01:16 +02001516 return 0;
1517
1518 smp->data.u.str.area = path.ptr;
1519 smp->data.u.str.data = path.len;
1520 smp->data.type = SMP_T_STR;
1521 smp->flags = SMP_F_CONST;
1522
1523 return 1;
1524}
1525
1526/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1527 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1528 */
1529static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1530{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001531 struct http_txn *txn;
1532
1533 if (!smp->strm)
1534 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001535
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001536 txn = smp->strm->txn;
Christopher Faulet09f88362021-04-01 16:00:29 +02001537 if (!txn || txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001538 return 0;
1539
1540 if (txn->req.flags & HTTP_MSGF_VER_11)
1541 smp->data.u.str.area = "HTTP/1.1";
1542 else
1543 smp->data.u.str.area = "HTTP/1.0";
1544
1545 smp->data.u.str.data = 8;
1546 smp->data.type = SMP_T_STR;
1547 smp->flags = SMP_F_CONST;
1548 return 1;
1549
1550}
1551
1552/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1553 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1554 */
1555static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1556{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001557 struct http_txn *txn;
1558
1559 if (!smp->strm)
1560 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001561
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001562 txn = smp->strm->txn;
Christopher Faulet09f88362021-04-01 16:00:29 +02001563 if (!txn || txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001564 return 0;
1565
1566 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1567 smp->data.u.str.area = "HTTP/1.1";
1568 else
1569 smp->data.u.str.area = "HTTP/1.0";
1570
1571 smp->data.u.str.data = 8;
1572 smp->data.type = SMP_T_STR;
1573 smp->flags = SMP_F_CONST;
1574 return 1;
1575
1576}
1577
1578/* Iterate over all cookies present in a message. The context is stored in
1579 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1580 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1581 * the direction, multiple cookies may be parsed on the same line or not.
Maciej Zdebdea7c202020-11-13 09:38:06 +00001582 * If provided, the searched cookie name is in args, in args->data.str. If
1583 * the input options indicate that no iterating is desired, then only last
1584 * value is fetched if any. If no cookie name is provided, the first cookie
1585 * value found is fetched. The returned sample is of type CSTR. Can be used
1586 * to parse cookies in other files.
Willy Tarreau79e57332018-10-02 16:01:16 +02001587 */
1588static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1589{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001590 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1591 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001592 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001593 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001594 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1595 struct ist hdr;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001596 char *cook = NULL;
1597 size_t cook_l = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001598 int found = 0;
1599
Christopher Faulet623af932021-01-29 11:22:15 +01001600 if (args->type == ARGT_STR) {
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001601 cook = args->data.str.area;
1602 cook_l = args->data.str.data;
1603 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001604
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001605 if (!ctx) {
1606 /* first call */
1607 ctx = &static_http_hdr_ctx;
1608 ctx->blk = NULL;
1609 smp->ctx.a[2] = ctx;
1610 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001611
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001612 if (!htx)
1613 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001614
Christopher Faulet16032ab2020-04-30 11:30:00 +02001615 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001616
Maciej Zdebdea7c202020-11-13 09:38:06 +00001617 /* OK so basically here, either we want only one value or we want to
1618 * iterate over all of them and we fetch the next one. In this last case
1619 * SMP_OPT_ITERATE option is set.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001620 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001621
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001622 if (!(smp->flags & SMP_F_NOT_LAST)) {
1623 /* search for the header from the beginning, we must first initialize
1624 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001625 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001626 smp->ctx.a[0] = NULL;
1627 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001628 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001629
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001630 smp->flags |= SMP_F_VOL_HDR;
1631 while (1) {
1632 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1633 if (!smp->ctx.a[0]) {
1634 if (!http_find_header(htx, hdr, ctx, 0))
1635 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001636
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001637 if (ctx->value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001638 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001639
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001640 smp->ctx.a[0] = ctx->value.ptr;
1641 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001642 }
1643
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001644 smp->data.type = SMP_T_STR;
1645 smp->flags |= SMP_F_CONST;
1646 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001647 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001648 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1649 &smp->data.u.str.area,
1650 &smp->data.u.str.data);
1651 if (smp->ctx.a[0]) {
1652 found = 1;
Maciej Zdebdea7c202020-11-13 09:38:06 +00001653 if (smp->opt & SMP_OPT_ITERATE) {
1654 /* iterate on cookie value */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001655 smp->flags |= SMP_F_NOT_LAST;
1656 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001657 }
Maciej Zdebdea7c202020-11-13 09:38:06 +00001658 if (args->data.str.data == 0) {
1659 /* No cookie name, first occurrence returned */
1660 break;
1661 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001662 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001663 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001664 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001665
Willy Tarreau79e57332018-10-02 16:01:16 +02001666 /* all cookie headers and values were scanned. If we're looking for the
1667 * last occurrence, we may return it now.
1668 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001669 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001670 smp->flags &= ~SMP_F_NOT_LAST;
1671 return found;
1672}
1673
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001674/* Same than smp_fetch_cookie() but only relies on the sample direction to
1675 * choose the right channel. So instead of duplicating the code, we just change
1676 * the keyword and then fallback on smp_fetch_cookie().
1677 */
1678static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1679{
1680 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1681 return smp_fetch_cookie(args, smp, kw, private);
1682}
1683
Willy Tarreau79e57332018-10-02 16:01:16 +02001684/* Iterate over all cookies present in a request to count how many occurrences
1685 * match the name in args and args->data.str.len. If <multi> is non-null, then
1686 * multiple cookies may be parsed on the same line. The returned sample is of
1687 * type UINT. Accepts exactly 1 argument of type string.
1688 */
1689static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1690{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001691 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1692 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001693 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001694 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001695 struct http_hdr_ctx ctx;
1696 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001697 char *val_beg, *val_end;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001698 char *cook = NULL;
1699 size_t cook_l = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001700 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001701
Christopher Faulet623af932021-01-29 11:22:15 +01001702 if (args->type == ARGT_STR){
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001703 cook = args->data.str.area;
1704 cook_l = args->data.str.data;
1705 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001706
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001707 if (!htx)
1708 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001709
Christopher Faulet16032ab2020-04-30 11:30:00 +02001710 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001711
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001712 val_end = val_beg = NULL;
1713 ctx.blk = NULL;
1714 cnt = 0;
1715 while (1) {
1716 /* Note: val_beg == NULL every time we need to fetch a new header */
1717 if (!val_beg) {
1718 if (!http_find_header(htx, hdr, &ctx, 0))
1719 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001720
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001721 if (ctx.value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001722 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001723
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001724 val_beg = ctx.value.ptr;
1725 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001726 }
1727
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001728 smp->data.type = SMP_T_STR;
1729 smp->flags |= SMP_F_CONST;
1730 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001731 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001732 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1733 &smp->data.u.str.area,
1734 &smp->data.u.str.data))) {
1735 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001736 }
1737 }
1738
1739 smp->data.type = SMP_T_SINT;
1740 smp->data.u.sint = cnt;
1741 smp->flags |= SMP_F_VOL_HDR;
1742 return 1;
1743}
1744
1745/* Fetch an cookie's integer value. The integer value is returned. It
1746 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1747 */
1748static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1749{
1750 int ret = smp_fetch_cookie(args, smp, kw, private);
1751
1752 if (ret > 0) {
1753 smp->data.type = SMP_T_SINT;
1754 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1755 smp->data.u.str.data);
1756 }
1757
1758 return ret;
1759}
1760
1761/************************************************************************/
1762/* The code below is dedicated to sample fetches */
1763/************************************************************************/
1764
1765/* This scans a URL-encoded query string. It takes an optionally wrapping
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001766 * string whose first contiguous chunk has its beginning in ctx->a[0] and end
Willy Tarreau79e57332018-10-02 16:01:16 +02001767 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1768 * pointers are updated for next iteration before leaving.
1769 */
1770static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1771{
1772 const char *vstart, *vend;
1773 struct buffer *temp;
1774 const char **chunks = (const char **)smp->ctx.a;
1775
1776 if (!http_find_next_url_param(chunks, name, name_len,
1777 &vstart, &vend, delim))
1778 return 0;
1779
1780 /* Create sample. If the value is contiguous, return the pointer as CONST,
1781 * if the value is wrapped, copy-it in a buffer.
1782 */
1783 smp->data.type = SMP_T_STR;
1784 if (chunks[2] &&
1785 vstart >= chunks[0] && vstart <= chunks[1] &&
1786 vend >= chunks[2] && vend <= chunks[3]) {
1787 /* Wrapped case. */
1788 temp = get_trash_chunk();
1789 memcpy(temp->area, vstart, chunks[1] - vstart);
1790 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1791 vend - chunks[2]);
1792 smp->data.u.str.area = temp->area;
1793 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1794 } else {
1795 /* Contiguous case. */
1796 smp->data.u.str.area = (char *)vstart;
1797 smp->data.u.str.data = vend - vstart;
1798 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1799 }
1800
1801 /* Update context, check wrapping. */
1802 chunks[0] = vend;
1803 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1804 chunks[1] = chunks[3];
1805 chunks[2] = NULL;
1806 }
1807
1808 if (chunks[0] < chunks[1])
1809 smp->flags |= SMP_F_NOT_LAST;
1810
1811 return 1;
1812}
1813
1814/* This function iterates over each parameter of the query string. It uses
1815 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1816 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1817 * An optional parameter name is passed in args[0], otherwise any parameter is
1818 * considered. It supports an optional delimiter argument for the beginning of
1819 * the string in args[1], which defaults to "?".
1820 */
1821static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1822{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001823 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001824 char delim = '?';
1825 const char *name;
1826 int name_len;
1827
Christopher Faulet623af932021-01-29 11:22:15 +01001828 if ((args[0].type && args[0].type != ARGT_STR) ||
Willy Tarreau79e57332018-10-02 16:01:16 +02001829 (args[1].type && args[1].type != ARGT_STR))
1830 return 0;
1831
1832 name = "";
1833 name_len = 0;
1834 if (args->type == ARGT_STR) {
1835 name = args->data.str.area;
1836 name_len = args->data.str.data;
1837 }
1838
1839 if (args[1].type)
1840 delim = *args[1].data.str.area;
1841
1842 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001843 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001844 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001845
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001846 if (!htx)
1847 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001848
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001849 sl = http_get_stline(htx);
1850 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1851 if (!smp->ctx.a[0])
1852 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001853
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001854 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001855
1856 /* Assume that the context is filled with NULL pointer
1857 * before the first call.
1858 * smp->ctx.a[2] = NULL;
1859 * smp->ctx.a[3] = NULL;
1860 */
1861 }
1862
1863 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1864}
1865
1866/* This function iterates over each parameter of the body. This requires
1867 * that the body has been waited for using http-buffer-request. It uses
1868 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001869 * contiguous part of the body, and optionally ctx->a[2..3] to reference the
Willy Tarreau79e57332018-10-02 16:01:16 +02001870 * optional second part if the body wraps at the end of the buffer. An optional
1871 * parameter name is passed in args[0], otherwise any parameter is considered.
1872 */
1873static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1874{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001875 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001876 const char *name;
1877 int name_len;
1878
Christopher Faulet623af932021-01-29 11:22:15 +01001879 if (args[0].type && args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001880 return 0;
1881
1882 name = "";
1883 name_len = 0;
1884 if (args[0].type == ARGT_STR) {
1885 name = args[0].data.str.area;
1886 name_len = args[0].data.str.data;
1887 }
1888
1889 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulete596d182020-05-05 17:46:34 +02001890 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001891 struct buffer *temp;
1892 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001893
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001894 if (!htx)
1895 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001896
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001897 temp = get_trash_chunk();
1898 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1899 struct htx_blk *blk = htx_get_blk(htx, pos);
1900 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001901
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001902 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001903 break;
1904 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001905 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001906 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001907 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001908 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001909
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001910 smp->ctx.a[0] = temp->area;
1911 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001912
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001913 /* Assume that the context is filled with NULL pointer
1914 * before the first call.
1915 * smp->ctx.a[2] = NULL;
1916 * smp->ctx.a[3] = NULL;
1917 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001918
Willy Tarreau79e57332018-10-02 16:01:16 +02001919 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001920
Willy Tarreau79e57332018-10-02 16:01:16 +02001921 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1922}
1923
1924/* Return the signed integer value for the specified url parameter (see url_param
1925 * above).
1926 */
1927static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1928{
1929 int ret = smp_fetch_url_param(args, smp, kw, private);
1930
1931 if (ret > 0) {
1932 smp->data.type = SMP_T_SINT;
1933 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1934 smp->data.u.str.data);
1935 }
1936
1937 return ret;
1938}
1939
1940/* This produces a 32-bit hash of the concatenation of the first occurrence of
1941 * the Host header followed by the path component if it begins with a slash ('/').
1942 * This means that '*' will not be added, resulting in exactly the first Host
1943 * entry. If no Host header is found, then the path is used. The resulting value
1944 * is hashed using the url hash followed by a full avalanche hash and provides a
1945 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1946 * high-traffic sites without having to store whole paths.
1947 * this differs from the base32 functions in that it includes the url parameters
1948 * as well as the path
1949 */
1950static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1951{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001952 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001953 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001954 struct http_hdr_ctx ctx;
1955 struct htx_sl *sl;
1956 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001957 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001958
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001959 if (!htx)
1960 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001961
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001962 ctx.blk = NULL;
1963 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1964 /* OK we have the header value in ctx.value */
1965 while (ctx.value.len--)
1966 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001967 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001968
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001969 /* now retrieve the path */
1970 sl = http_get_stline(htx);
1971 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001972 if (path.len && *(path.ptr) == '/') {
1973 while (path.len--)
1974 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001975 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001976
Willy Tarreau79e57332018-10-02 16:01:16 +02001977 hash = full_hash(hash);
1978
1979 smp->data.type = SMP_T_SINT;
1980 smp->data.u.sint = hash;
1981 smp->flags = SMP_F_VOL_1ST;
1982 return 1;
1983}
1984
1985/* This concatenates the source address with the 32-bit hash of the Host and
1986 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1987 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1988 * on the source address length. The URL hash is stored before the address so
1989 * that in environments where IPv6 is insignificant, truncating the output to
1990 * 8 bytes would still work.
1991 */
1992static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1993{
1994 struct buffer *temp;
1995 struct connection *cli_conn = objt_conn(smp->sess->origin);
1996
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001997 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001998 return 0;
1999
2000 if (!smp_fetch_url32(args, smp, kw, private))
2001 return 0;
2002
2003 temp = get_trash_chunk();
2004 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2005 temp->data += sizeof(unsigned int);
2006
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002007 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02002008 case AF_INET:
2009 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002010 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002011 4);
2012 temp->data += 4;
2013 break;
2014 case AF_INET6:
2015 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002016 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002017 16);
2018 temp->data += 16;
2019 break;
2020 default:
2021 return 0;
2022 }
2023
2024 smp->data.u.str = *temp;
2025 smp->data.type = SMP_T_BIN;
2026 return 1;
2027}
2028
2029/************************************************************************/
2030/* Other utility functions */
2031/************************************************************************/
2032
2033/* This function is used to validate the arguments passed to any "hdr" fetch
2034 * keyword. These keywords support an optional positive or negative occurrence
2035 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2036 * is assumed that the types are already the correct ones. Returns 0 on error,
2037 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2038 * error message in case of error, that the caller is responsible for freeing.
2039 * The initial location must either be freeable or NULL.
2040 * Note: this function's pointer is checked from Lua.
2041 */
2042int val_hdr(struct arg *arg, char **err_msg)
2043{
2044 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2045 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2046 return 0;
2047 }
2048 return 1;
2049}
2050
2051/************************************************************************/
2052/* All supported sample fetch keywords must be declared here. */
2053/************************************************************************/
2054
2055/* Note: must not be declared <const> as its list will be overwritten */
2056static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2057 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2058 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2059 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Yves Lafonb4d37082021-02-11 11:01:28 +01002060 { "baseq", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002061
2062 /* capture are allocated and are permanent in the stream */
2063 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2064
2065 /* retrieve these captures from the HTTP logs */
2066 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2067 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2068 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2069
2070 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2071 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2072
2073 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2074 * are only here to match the ACL's name, are request-only and are used
2075 * for ACL compatibility only.
2076 */
2077 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002078 { "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 +02002079 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2080 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2081
2082 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2083 * only here to match the ACL's name, are request-only and are used for
2084 * ACL compatibility only.
2085 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002086 { "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 +02002087 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2088 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2089 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2090
Christopher Fauleta4063562019-08-02 11:51:37 +02002091 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2092 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2093 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002094 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2095 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2096 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2097 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2098 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Faulete720c322020-09-02 17:25:18 +02002099 { "pathq", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002100 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2101
2102 /* HTTP protocol on the request path */
2103 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2104 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2105
2106 /* HTTP version on the request path */
2107 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2108 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2109
2110 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2111 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2112 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2113 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2114
2115 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2116 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2117
2118 /* HTTP version on the response path */
2119 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2120 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2121
Christopher Faulete596d182020-05-05 17:46:34 +02002122 { "res.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2123 { "res.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2124 { "res.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2125
2126 { "res.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2127 { "res.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2128
Willy Tarreau79e57332018-10-02 16:01:16 +02002129 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2130 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2131 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2132 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2133
2134 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2135 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2136 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2137 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2138 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2139 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2140 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2141
2142 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2143 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2144 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2145 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2146
2147 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2148 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2149 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2150 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2151 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2152 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2153 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2154
2155 /* scook is valid only on the response and is used for ACL compatibility */
2156 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2157 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2158 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2159 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2160
2161 /* shdr is valid only on the response and is used for ACL compatibility */
2162 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2163 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2164 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2165 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2166
2167 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2168 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2169 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2170 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2171 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2172 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2173 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2174 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2175 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2176 { "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 +02002177
Willy Tarreau79e57332018-10-02 16:01:16 +02002178 { /* END */ },
2179}};
2180
Willy Tarreau0108d902018-11-25 19:14:37 +01002181INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002182
2183/*
2184 * Local variables:
2185 * c-indent-level: 8
2186 * c-basic-offset: 8
2187 * End:
2188 */