blob: 2bb327c1704a94733a8facb7ae9ce1a45c20125c [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;
Willy Tarreauf7575fc2022-10-04 09:18:34 +0200333 struct htx *htx = NULL;
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;
Christopher Fauletd44b2d42022-10-04 08:58:02 +0200345 meth = txn->meth;
Christopher Faulet5b1cf0c2022-06-22 17:16:41 +0200346 }
347
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200348 smp->data.type = SMP_T_METH;
349 smp->data.u.meth.meth = meth;
350 if (meth == HTTP_METH_OTHER) {
351 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200352
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200353 sl = http_get_stline(htx);
354 smp->flags |= SMP_F_CONST;
355 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
356 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200357 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200358 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200359 return 1;
360}
361
362static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
363{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200364 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200365 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200366 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200367 char *ptr;
368 int len;
369
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200370 if (!htx)
371 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200372
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200373 sl = http_get_stline(htx);
374 len = HTX_SL_REQ_VLEN(sl);
375 ptr = HTX_SL_REQ_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200376
377 while ((len-- > 0) && (*ptr++ != '/'));
378 if (len <= 0)
379 return 0;
380
381 smp->data.type = SMP_T_STR;
382 smp->data.u.str.area = ptr;
383 smp->data.u.str.data = len;
384
385 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
386 return 1;
387}
388
389static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
390{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200391 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200392 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200393 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200394 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200395 char *ptr;
396 int len;
397
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200398 if (!htx)
399 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200400
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200401 sl = http_get_stline(htx);
402 len = HTX_SL_RES_VLEN(sl);
403 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200404
405 while ((len-- > 0) && (*ptr++ != '/'));
406 if (len <= 0)
407 return 0;
408
409 smp->data.type = SMP_T_STR;
410 smp->data.u.str.area = ptr;
411 smp->data.u.str.data = len;
412
413 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
414 return 1;
415}
416
417/* 3. Check on Status Code. We manipulate integers here. */
418static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
419{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200420 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200421 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200422 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200423 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200424 char *ptr;
425 int len;
426
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200427 if (!htx)
428 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200429
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200430 sl = http_get_stline(htx);
431 len = HTX_SL_RES_CLEN(sl);
432 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200433
434 smp->data.type = SMP_T_SINT;
435 smp->data.u.sint = __strl2ui(ptr, len);
436 smp->flags = SMP_F_VOL_1ST;
437 return 1;
438}
439
440static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
441{
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100442 struct ist unique_id;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100443
Willy Tarreau79e57332018-10-02 16:01:16 +0200444 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
445 return 0;
446
Willy Tarreaua1062a42020-04-29 11:50:38 +0200447 if (!smp->strm)
448 return 0;
449
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100450 unique_id = stream_generate_unique_id(smp->strm, &smp->sess->fe->format_unique_id);
451 if (!isttest(unique_id))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100452 return 0;
453
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100454 smp->data.u.str.area = smp->strm->unique_id.ptr;
455 smp->data.u.str.data = smp->strm->unique_id.len;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100456 smp->data.type = SMP_T_STR;
Willy Tarreau79e57332018-10-02 16:01:16 +0200457 smp->flags = SMP_F_CONST;
458 return 1;
459}
460
461/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800462 * empty line which separes headers from the body. This is useful
463 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200464 */
465static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
466{
Christopher Faulete596d182020-05-05 17:46:34 +0200467 /* possible keywords: req.hdrs, res.hdrs */
468 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200469 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200470 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200471 struct buffer *temp;
472 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200473
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200474 if (!htx)
475 return 0;
476 temp = get_trash_chunk();
477 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
478 struct htx_blk *blk = htx_get_blk(htx, pos);
479 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200480
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200481 if (type == HTX_BLK_HDR) {
482 struct ist n = htx_get_blk_name(htx, blk);
483 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200484
Christopher Faulet53a899b2019-10-08 16:38:42 +0200485 if (!h1_format_htx_hdr(n, v, temp))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200486 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200487 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200488 else if (type == HTX_BLK_EOH) {
489 if (!chunk_memcat(temp, "\r\n", 2))
490 return 0;
491 break;
492 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200493 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200494 smp->data.type = SMP_T_STR;
495 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200496 return 1;
497}
498
499/* Returns the header request in a length/value encoded format.
500 * This is useful for exchanges with the SPOE.
501 *
502 * A "length value" is a multibyte code encoding numbers. It uses the
503 * SPOE format. The encoding is the following:
504 *
505 * Each couple "header name" / "header value" is composed
506 * like this:
507 * "length value" "header name bytes"
508 * "length value" "header value bytes"
509 * When the last header is reached, the header name and the header
510 * value are empty. Their length are 0
511 */
512static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
513{
Christopher Faulete596d182020-05-05 17:46:34 +0200514 /* possible keywords: req.hdrs_bin, res.hdrs_bin */
515 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200516 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200517 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200518 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200519 char *p, *end;
520 int32_t pos;
521 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200522
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200523 if (!htx)
524 return 0;
525 temp = get_trash_chunk();
526 p = temp->area;
527 end = temp->area + temp->size;
528 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
529 struct htx_blk *blk = htx_get_blk(htx, pos);
530 enum htx_blk_type type = htx_get_blk_type(blk);
531 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200532
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200533 if (type == HTX_BLK_HDR) {
534 n = htx_get_blk_name(htx,blk);
535 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200536
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200537 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200538 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200539 if (ret == -1)
540 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200541 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200542 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200543 memcpy(p, n.ptr, n.len);
544 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200545
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200546 /* encode the header value. */
547 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200548 if (ret == -1)
549 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200550 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200551 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200552 memcpy(p, v.ptr, v.len);
553 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200554
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200555 }
556 else if (type == HTX_BLK_EOH) {
557 /* encode the end of the header list with empty
558 * header name and header value.
559 */
560 ret = encode_varint(0, &p, end);
561 if (ret == -1)
562 return 0;
563 ret = encode_varint(0, &p, end);
564 if (ret == -1)
565 return 0;
566 break;
567 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200568 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200569
570 /* Initialise sample data which will be filled. */
571 smp->data.type = SMP_T_BIN;
572 smp->data.u.str.area = temp->area;
573 smp->data.u.str.data = p - temp->area;
574 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200575 return 1;
576}
577
578/* returns the longest available part of the body. This requires that the body
579 * has been waited for using http-buffer-request.
580 */
581static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
582{
Christopher Faulete596d182020-05-05 17:46:34 +0200583 /* possible keywords: req.body, res.body */
584 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200585 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200586 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200587 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200588 int32_t pos;
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100589 int finished = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200590
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200591 if (!htx)
592 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200593
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200594 temp = get_trash_chunk();
595 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
596 struct htx_blk *blk = htx_get_blk(htx, pos);
597 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200598
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100599 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) {
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100600 finished = 1;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200601 break;
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100602 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200603 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +0200604 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200605 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200606 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200607 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200608
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200609 smp->data.type = SMP_T_BIN;
610 smp->data.u.str = *temp;
611 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200612
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100613 if (!finished && (check || (chn && !channel_full(chn, global.tune.maxrewrite) &&
614 !(chn->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)))))
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200615 smp->flags |= SMP_F_MAY_CHANGE;
616
Willy Tarreau79e57332018-10-02 16:01:16 +0200617 return 1;
618}
619
620
621/* returns the available length of the body. This requires that the body
622 * has been waited for using http-buffer-request.
623 */
624static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
625{
Christopher Faulete596d182020-05-05 17:46:34 +0200626 /* possible keywords: req.body_len, res.body_len */
627 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200628 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200629 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200630 int32_t pos;
631 unsigned long long len = 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100632
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200633 if (!htx)
634 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100635
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200636 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
637 struct htx_blk *blk = htx_get_blk(htx, pos);
638 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100639
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100640 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200641 break;
642 if (type == HTX_BLK_DATA)
643 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200644 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200645
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200646 smp->data.type = SMP_T_SINT;
647 smp->data.u.sint = len;
648 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200649 return 1;
650}
651
652
653/* returns the advertised length of the body, or the advertised size of the
654 * chunks available in the buffer. This requires that the body has been waited
655 * for using http-buffer-request.
656 */
657static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
658{
Christopher Faulete596d182020-05-05 17:46:34 +0200659 /* possible keywords: req.body_size, res.body_size */
660 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200661 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200662 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200663 int32_t pos;
664 unsigned long long len = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200665
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200666 if (!htx)
667 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100668
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200669 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
670 struct htx_blk *blk = htx_get_blk(htx, pos);
671 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100672
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100673 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200674 break;
675 if (type == HTX_BLK_DATA)
676 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200677 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200678 if (htx->extra != ULLONG_MAX)
679 len += htx->extra;
Willy Tarreau79e57332018-10-02 16:01:16 +0200680
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200681 smp->data.type = SMP_T_SINT;
682 smp->data.u.sint = len;
683 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200684 return 1;
685}
686
687
688/* 4. Check on URL/URI. A pointer to the URI is stored. */
689static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
690{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200691 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200692 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200693 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200694
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200695 if (!htx)
696 return 0;
697 sl = http_get_stline(htx);
698 smp->data.type = SMP_T_STR;
699 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
700 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
701 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200702 return 1;
703}
704
705static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
706{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200707 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200708 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200709 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200710 struct sockaddr_storage addr;
711
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200712 memset(&addr, 0, sizeof(addr));
713
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200714 if (!htx)
715 return 0;
716 sl = http_get_stline(htx);
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200717 if (url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL) < 0)
718 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200719
Willy Tarreau48584642021-05-09 10:32:54 +0200720 if (addr.ss_family != AF_INET)
Willy Tarreau79e57332018-10-02 16:01:16 +0200721 return 0;
722
723 smp->data.type = SMP_T_IPV4;
724 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
725 smp->flags = 0;
726 return 1;
727}
728
729static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
730{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200731 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200732 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200733 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200734 struct sockaddr_storage addr;
735
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200736 memset(&addr, 0, sizeof(addr));
737
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200738 if (!htx)
739 return 0;
740 sl = http_get_stline(htx);
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200741 if (url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL) < 0)
742 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200743
Willy Tarreau48584642021-05-09 10:32:54 +0200744 if (addr.ss_family != AF_INET)
Willy Tarreau79e57332018-10-02 16:01:16 +0200745 return 0;
746
747 smp->data.type = SMP_T_SINT;
Willy Tarreau48584642021-05-09 10:32:54 +0200748 smp->data.u.sint = get_host_port(&addr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200749 smp->flags = 0;
750 return 1;
751}
752
753/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
754 * Accepts an optional argument of type string containing the header field name,
755 * and an optional argument of type signed or unsigned integer to request an
756 * explicit occurrence of the header. Note that in the event of a missing name,
757 * headers are considered from the first one. It does not stop on commas and
758 * returns full lines instead (useful for User-Agent or Date for example).
759 */
760static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
761{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200762 /* possible keywords: req.fhdr, res.fhdr */
763 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200764 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200765 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200766 struct http_hdr_ctx *ctx = smp->ctx.a[0];
767 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200768 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200769
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200770 if (!ctx) {
771 /* first call */
772 ctx = &static_http_hdr_ctx;
773 ctx->blk = NULL;
774 smp->ctx.a[0] = ctx;
775 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200776
Christopher Faulet623af932021-01-29 11:22:15 +0100777 if (args[0].type != ARGT_STR)
778 return 0;
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100779 name = ist2(args[0].data.str.area, args[0].data.str.data);
Willy Tarreau79e57332018-10-02 16:01:16 +0200780
Christopher Faulet623af932021-01-29 11:22:15 +0100781 if (args[1].type == ARGT_SINT)
782 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200783
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200784 if (!htx)
785 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200786
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200787 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
788 /* search for header from the beginning */
789 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200790
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200791 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
792 /* no explicit occurrence and single fetch => last header by default */
793 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200794
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200795 if (!occ)
796 /* prepare to report multiple occurrences for ACL fetches */
797 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200798
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200799 smp->data.type = SMP_T_STR;
800 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
801 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
802 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200803 smp->flags &= ~SMP_F_NOT_LAST;
804 return 0;
805}
806
807/* 6. Check on HTTP header count. The number of occurrences is returned.
808 * Accepts exactly 1 argument of type string. It does not stop on commas and
809 * returns full lines instead (useful for User-Agent or Date for example).
810 */
811static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
812{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200813 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
814 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200815 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200816 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200817 struct http_hdr_ctx ctx;
818 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200819 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200820
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200821 if (!htx)
822 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200823
Christopher Faulet623af932021-01-29 11:22:15 +0100824 if (args->type == ARGT_STR) {
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100825 name = ist2(args->data.str.area, args->data.str.data);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200826 } else {
Tim Duesterhus68a088d2021-02-28 16:11:37 +0100827 name = IST_NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200828 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200829
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200830 ctx.blk = NULL;
831 cnt = 0;
832 while (http_find_header(htx, name, &ctx, 1))
833 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200834 smp->data.type = SMP_T_SINT;
835 smp->data.u.sint = cnt;
836 smp->flags = SMP_F_VOL_HDR;
837 return 1;
838}
839
840static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
841{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200842 /* possible keywords: req.hdr_names, res.hdr_names */
843 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200844 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200845 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200846 struct buffer *temp;
847 char del = ',';
848
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200849 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200850
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200851 if (!htx)
852 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200853
Christopher Faulet623af932021-01-29 11:22:15 +0100854 if (args->type == ARGT_STR)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200855 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200856
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200857 temp = get_trash_chunk();
858 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
859 struct htx_blk *blk = htx_get_blk(htx, pos);
860 enum htx_blk_type type = htx_get_blk_type(blk);
861 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200862
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200863 if (type == HTX_BLK_EOH)
864 break;
865 if (type != HTX_BLK_HDR)
866 continue;
867 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200868
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200869 if (temp->data)
870 temp->area[temp->data++] = del;
871 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200872 }
873
874 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200875 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200876 smp->flags = SMP_F_VOL_HDR;
877 return 1;
878}
879
880/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
881 * Accepts an optional argument of type string containing the header field name,
882 * and an optional argument of type signed or unsigned integer to request an
883 * explicit occurrence of the header. Note that in the event of a missing name,
884 * headers are considered from the first one.
885 */
886static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
887{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200888 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
889 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200890 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200891 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200892 struct http_hdr_ctx *ctx = smp->ctx.a[0];
893 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200894 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200895
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200896 if (!ctx) {
897 /* first call */
898 ctx = &static_http_hdr_ctx;
899 ctx->blk = NULL;
900 smp->ctx.a[0] = ctx;
901 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200902
Christopher Faulet623af932021-01-29 11:22:15 +0100903 if (args[0].type != ARGT_STR)
904 return 0;
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100905 name = ist2(args[0].data.str.area, args[0].data.str.data);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200906
Christopher Faulet623af932021-01-29 11:22:15 +0100907 if (args[1].type == ARGT_SINT)
908 occ = args[1].data.sint;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200909
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200910 if (!htx)
911 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200912
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200913 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
914 /* search for header from the beginning */
915 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200916
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200917 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
918 /* no explicit occurrence and single fetch => last header by default */
919 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200920
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200921 if (!occ)
922 /* prepare to report multiple occurrences for ACL fetches */
923 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200924
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200925 smp->data.type = SMP_T_STR;
926 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
927 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
928 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200929
930 smp->flags &= ~SMP_F_NOT_LAST;
931 return 0;
932}
933
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200934/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
935 * the right channel. So instead of duplicating the code, we just change the
936 * keyword and then fallback on smp_fetch_hdr().
937 */
938static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
939{
940 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
941 return smp_fetch_hdr(args, smp, kw, private);
942}
943
Willy Tarreau79e57332018-10-02 16:01:16 +0200944/* 6. Check on HTTP header count. The number of occurrences is returned.
945 * Accepts exactly 1 argument of type string.
946 */
947static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
948{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200949 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
950 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200951 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200952 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200953 struct http_hdr_ctx ctx;
954 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200955 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200956
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200957 if (!htx)
958 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200959
Christopher Faulet623af932021-01-29 11:22:15 +0100960 if (args->type == ARGT_STR) {
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100961 name = ist2(args->data.str.area, args->data.str.data);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200962 } else {
Tim Duesterhus68a088d2021-02-28 16:11:37 +0100963 name = IST_NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200964 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200965
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200966 ctx.blk = NULL;
967 cnt = 0;
968 while (http_find_header(htx, name, &ctx, 0))
969 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200970
971 smp->data.type = SMP_T_SINT;
972 smp->data.u.sint = cnt;
973 smp->flags = SMP_F_VOL_HDR;
974 return 1;
975}
976
977/* Fetch an HTTP header's integer value. The integer value is returned. It
978 * takes a mandatory argument of type string and an optional one of type int
979 * to designate a specific occurrence. It returns an unsigned integer, which
980 * may or may not be appropriate for everything.
981 */
982static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
983{
984 int ret = smp_fetch_hdr(args, smp, kw, private);
985
986 if (ret > 0) {
987 smp->data.type = SMP_T_SINT;
988 smp->data.u.sint = strl2ic(smp->data.u.str.area,
989 smp->data.u.str.data);
990 }
991
992 return ret;
993}
994
995/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
996 * and an optional one of type int to designate a specific occurrence.
Willy Tarreau7b0e00d2021-03-25 14:12:29 +0100997 * It returns an IPv4 or IPv6 address. Addresses surrounded by invalid chars
998 * are rejected. However IPv4 addresses may be followed with a colon and a
999 * valid port number.
Willy Tarreau79e57332018-10-02 16:01:16 +02001000 */
1001static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1002{
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001003 struct buffer *temp = get_trash_chunk();
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001004 int ret, len;
1005 int port;
Willy Tarreau79e57332018-10-02 16:01:16 +02001006
1007 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001008 if (smp->data.u.str.data < temp->size - 1) {
1009 memcpy(temp->area, smp->data.u.str.area,
1010 smp->data.u.str.data);
1011 temp->area[smp->data.u.str.data] = '\0';
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001012 len = url2ipv4((char *) temp->area, &smp->data.u.ipv4);
Willy Tarreau645dc082021-03-31 11:41:36 +02001013 if (len > 0 && len == smp->data.u.str.data) {
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001014 /* plain IPv4 address */
1015 smp->data.type = SMP_T_IPV4;
1016 break;
1017 } else if (len > 0 && temp->area[len] == ':' &&
1018 strl2irc(temp->area + len + 1, smp->data.u.str.data - len - 1, &port) == 0 &&
1019 port >= 0 && port <= 65535) {
1020 /* IPv4 address suffixed with ':' followed by a valid port number */
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001021 smp->data.type = SMP_T_IPV4;
1022 break;
1023 } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1024 smp->data.type = SMP_T_IPV6;
1025 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001026 }
1027 }
1028
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001029 /* if the header doesn't match an IP address, fetch next one */
1030 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001031 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001032 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001033 return ret;
1034}
Willy Tarreau79e57332018-10-02 16:01:16 +02001035
Christopher Faulete720c322020-09-02 17:25:18 +02001036/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at the
1037 * first '/' after the possible hostname. It ends before the possible '?' except
1038 * for 'pathq' keyword.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001039 */
1040static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1041{
1042 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001043 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001044 struct htx_sl *sl;
1045 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001046
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001047 if (!htx)
1048 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001049
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001050 sl = http_get_stline(htx);
Christopher Faulete720c322020-09-02 17:25:18 +02001051 path = http_get_path(htx_sl_req_uri(sl));
1052
Yves Lafonb4d37082021-02-11 11:01:28 +01001053 if (kw[4] == 'q' && (kw[0] == 'p' || kw[0] == 'b')) // pathq or baseq
Christopher Faulete720c322020-09-02 17:25:18 +02001054 path = http_get_path(htx_sl_req_uri(sl));
1055 else
1056 path = iststop(http_get_path(htx_sl_req_uri(sl)), '?');
1057
Tim Duesterhused526372020-03-05 17:56:33 +01001058 if (!isttest(path))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001059 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001060
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001061 /* OK, we got the '/' ! */
1062 smp->data.type = SMP_T_STR;
1063 smp->data.u.str.area = path.ptr;
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001064 smp->data.u.str.data = path.len;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001065 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001066 return 1;
1067}
1068
1069/* This produces a concatenation of the first occurrence of the Host header
1070 * followed by the path component if it begins with a slash ('/'). This means
1071 * that '*' will not be added, resulting in exactly the first Host entry.
1072 * If no Host header is found, then the path is returned as-is. The returned
1073 * value is stored in the trash so it does not need to be marked constant.
1074 * The returned sample is of type string.
1075 */
1076static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1077{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001078 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001079 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001080 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001081 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001082 struct http_hdr_ctx ctx;
1083 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001084
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001085 if (!htx)
1086 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001087
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001088 ctx.blk = NULL;
1089 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1090 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001091
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001092 /* OK we have the header value in ctx.value */
1093 temp = get_trash_chunk();
1094 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001095
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001096 /* now retrieve the path */
1097 sl = http_get_stline(htx);
1098 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001099 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001100 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001101
Yves Lafonb4d37082021-02-11 11:01:28 +01001102 if (kw[4] == 'q' && kw[0] == 'b') { // baseq
1103 len = path.len;
1104 } else {
1105 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1106 ;
1107 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001108
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001109 if (len && *(path.ptr) == '/')
1110 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001111 }
1112
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001113 smp->data.type = SMP_T_STR;
1114 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001115 smp->flags = SMP_F_VOL_1ST;
1116 return 1;
1117}
1118
1119/* This produces a 32-bit hash of the concatenation of the first occurrence of
1120 * the Host header followed by the path component if it begins with a slash ('/').
1121 * This means that '*' will not be added, resulting in exactly the first Host
1122 * entry. If no Host header is found, then the path is used. The resulting value
1123 * is hashed using the path hash followed by a full avalanche hash and provides a
1124 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1125 * high-traffic sites without having to store whole paths.
1126 */
1127static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1128{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001129 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001130 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001131 struct htx_sl *sl;
1132 struct http_hdr_ctx ctx;
1133 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001134 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001135
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001136 if (!htx)
1137 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001138
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001139 ctx.blk = NULL;
1140 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1141 /* OK we have the header value in ctx.value */
1142 while (ctx.value.len--)
1143 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001144 }
1145
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001146 /* now retrieve the path */
1147 sl = http_get_stline(htx);
1148 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001149 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001150 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001151
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001152 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1153 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001154
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001155 if (len && *(path.ptr) == '/') {
1156 while (len--)
1157 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001158 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001159 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001160
Willy Tarreau79e57332018-10-02 16:01:16 +02001161 hash = full_hash(hash);
1162
1163 smp->data.type = SMP_T_SINT;
1164 smp->data.u.sint = hash;
1165 smp->flags = SMP_F_VOL_1ST;
1166 return 1;
1167}
1168
1169/* This concatenates the source address with the 32-bit hash of the Host and
1170 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1171 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1172 * on the source address length. The path hash is stored before the address so
1173 * that in environments where IPv6 is insignificant, truncating the output to
1174 * 8 bytes would still work.
1175 */
1176static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1177{
1178 struct buffer *temp;
1179 struct connection *cli_conn = objt_conn(smp->sess->origin);
1180
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001181 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001182 return 0;
1183
1184 if (!smp_fetch_base32(args, smp, kw, private))
1185 return 0;
1186
1187 temp = get_trash_chunk();
1188 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1189 temp->data += sizeof(unsigned int);
1190
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001191 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001192 case AF_INET:
1193 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001194 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001195 4);
1196 temp->data += 4;
1197 break;
1198 case AF_INET6:
1199 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001200 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001201 16);
1202 temp->data += 16;
1203 break;
1204 default:
1205 return 0;
1206 }
1207
1208 smp->data.u.str = *temp;
1209 smp->data.type = SMP_T_BIN;
1210 return 1;
1211}
1212
1213/* Extracts the query string, which comes after the question mark '?'. If no
1214 * question mark is found, nothing is returned. Otherwise it returns a sample
1215 * of type string carrying the whole query string.
1216 */
1217static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1218{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001219 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001220 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001221 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001222 char *ptr, *end;
1223
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001224 if (!htx)
1225 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001226
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001227 sl = http_get_stline(htx);
1228 ptr = HTX_SL_REQ_UPTR(sl);
1229 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001230
1231 /* look up the '?' */
1232 do {
1233 if (ptr == end)
1234 return 0;
1235 } while (*ptr++ != '?');
1236
1237 smp->data.type = SMP_T_STR;
1238 smp->data.u.str.area = ptr;
1239 smp->data.u.str.data = end - ptr;
1240 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1241 return 1;
1242}
1243
1244static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1245{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001246 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001247 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001248
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001249 if (!htx)
1250 return 0;
1251 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001252 smp->data.u.sint = 1;
1253 return 1;
1254}
1255
1256/* return a valid test if the current request is the first one on the connection */
1257static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1258{
Willy Tarreau79512b62020-04-29 11:52:13 +02001259 if (!smp->strm)
1260 return 0;
1261
Willy Tarreau79e57332018-10-02 16:01:16 +02001262 smp->data.type = SMP_T_BOOL;
1263 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1264 return 1;
1265}
1266
Christopher Fauleta4063562019-08-02 11:51:37 +02001267/* Fetch the authentication method if there is an Authorization header. It
1268 * relies on get_http_auth()
1269 */
1270static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1271{
1272 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001273 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001274 struct http_txn *txn;
1275
1276 if (!htx)
1277 return 0;
1278
1279 txn = smp->strm->txn;
1280 if (!get_http_auth(smp, htx))
1281 return 0;
1282
1283 switch (txn->auth.method) {
1284 case HTTP_AUTH_BASIC:
1285 smp->data.u.str.area = "Basic";
1286 smp->data.u.str.data = 5;
1287 break;
1288 case HTTP_AUTH_DIGEST:
1289 /* Unexpected because not supported */
1290 smp->data.u.str.area = "Digest";
1291 smp->data.u.str.data = 6;
1292 break;
1293 default:
1294 return 0;
1295 }
1296
1297 smp->data.type = SMP_T_STR;
1298 smp->flags = SMP_F_CONST;
1299 return 1;
1300}
1301
1302/* Fetch the user supplied if there is an Authorization header. It relies on
1303 * get_http_auth()
1304 */
1305static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1306{
1307 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001308 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001309 struct http_txn *txn;
1310
1311 if (!htx)
1312 return 0;
1313
1314 txn = smp->strm->txn;
1315 if (!get_http_auth(smp, htx))
1316 return 0;
1317
1318 smp->data.type = SMP_T_STR;
1319 smp->data.u.str.area = txn->auth.user;
1320 smp->data.u.str.data = strlen(txn->auth.user);
1321 smp->flags = SMP_F_CONST;
1322 return 1;
1323}
1324
1325/* Fetch the password supplied if there is an Authorization header. It relies on
1326 * get_http_auth()
1327 */
1328static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1329{
1330 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001331 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001332 struct http_txn *txn;
1333
1334 if (!htx)
1335 return 0;
1336
1337 txn = smp->strm->txn;
1338 if (!get_http_auth(smp, htx))
1339 return 0;
1340
1341 smp->data.type = SMP_T_STR;
1342 smp->data.u.str.area = txn->auth.pass;
1343 smp->data.u.str.data = strlen(txn->auth.pass);
1344 smp->flags = SMP_F_CONST;
1345 return 1;
1346}
1347
Willy Tarreau79e57332018-10-02 16:01:16 +02001348/* Accepts exactly 1 argument of type userlist */
1349static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1350{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001351 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001352 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001353
Christopher Faulet623af932021-01-29 11:22:15 +01001354 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001355 return 0;
1356
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001357 if (!htx)
1358 return 0;
1359 if (!get_http_auth(smp, htx))
1360 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001361
1362 smp->data.type = SMP_T_BOOL;
1363 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001364 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001365 return 1;
1366}
1367
1368/* Accepts exactly 1 argument of type userlist */
1369static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1370{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001371 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001372 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001373
Christopher Faulet623af932021-01-29 11:22:15 +01001374 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001375 return 0;
1376
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001377 if (!htx)
1378 return 0;
1379 if (!get_http_auth(smp, htx))
1380 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001381
Willy Tarreau79e57332018-10-02 16:01:16 +02001382 /* if the user does not belong to the userlist or has a wrong password,
1383 * report that it unconditionally does not match. Otherwise we return
1384 * a string containing the username.
1385 */
1386 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1387 smp->strm->txn->auth.pass))
1388 return 0;
1389
1390 /* pat_match_auth() will need the user list */
1391 smp->ctx.a[0] = args->data.usr;
1392
1393 smp->data.type = SMP_T_STR;
1394 smp->flags = SMP_F_CONST;
1395 smp->data.u.str.area = smp->strm->txn->auth.user;
1396 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1397
1398 return 1;
1399}
1400
1401/* Fetch a captured HTTP request header. The index is the position of
1402 * the "capture" option in the configuration file
1403 */
1404static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1405{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001406 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001407 int idx;
1408
Christopher Faulet623af932021-01-29 11:22:15 +01001409 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001410 return 0;
1411
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001412 if (!smp->strm)
1413 return 0;
1414
1415 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001416 idx = args->data.sint;
1417
1418 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1419 return 0;
1420
1421 smp->data.type = SMP_T_STR;
1422 smp->flags |= SMP_F_CONST;
1423 smp->data.u.str.area = smp->strm->req_cap[idx];
1424 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1425
1426 return 1;
1427}
1428
1429/* Fetch a captured HTTP response header. The index is the position of
1430 * the "capture" option in the configuration file
1431 */
1432static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1433{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001434 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001435 int idx;
1436
Christopher Faulet623af932021-01-29 11:22:15 +01001437 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001438 return 0;
1439
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001440 if (!smp->strm)
1441 return 0;
1442
1443 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001444 idx = args->data.sint;
1445
1446 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1447 return 0;
1448
1449 smp->data.type = SMP_T_STR;
1450 smp->flags |= SMP_F_CONST;
1451 smp->data.u.str.area = smp->strm->res_cap[idx];
1452 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1453
1454 return 1;
1455}
1456
1457/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1458static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1459{
1460 struct buffer *temp;
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001461 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001462 char *ptr;
1463
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001464 if (!smp->strm)
1465 return 0;
1466
1467 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001468 if (!txn || !txn->uri)
1469 return 0;
1470
1471 ptr = txn->uri;
1472
1473 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1474 ptr++;
1475
1476 temp = get_trash_chunk();
1477 temp->area = txn->uri;
1478 temp->data = ptr - txn->uri;
1479 smp->data.u.str = *temp;
1480 smp->data.type = SMP_T_STR;
1481 smp->flags = SMP_F_CONST;
1482
1483 return 1;
1484
1485}
1486
1487/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1488static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1489{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001490 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001491 struct ist path;
1492 const char *ptr;
1493
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001494 if (!smp->strm)
1495 return 0;
1496
1497 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001498 if (!txn || !txn->uri)
1499 return 0;
1500
1501 ptr = txn->uri;
1502
1503 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1504 ptr++;
1505
1506 if (!*ptr)
1507 return 0;
1508
Christopher Faulet78337bb2018-11-15 14:35:18 +01001509 /* skip the first space and find space after URI */
1510 path = ist2(++ptr, 0);
1511 while (*ptr != ' ' && *ptr != '\0')
1512 ptr++;
1513 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001514
Christopher Faulet78337bb2018-11-15 14:35:18 +01001515 path = http_get_path(path);
Tim Duesterhused526372020-03-05 17:56:33 +01001516 if (!isttest(path))
Willy Tarreau79e57332018-10-02 16:01:16 +02001517 return 0;
1518
1519 smp->data.u.str.area = path.ptr;
1520 smp->data.u.str.data = path.len;
1521 smp->data.type = SMP_T_STR;
1522 smp->flags = SMP_F_CONST;
1523
1524 return 1;
1525}
1526
1527/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1528 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1529 */
1530static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1531{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001532 struct http_txn *txn;
1533
1534 if (!smp->strm)
1535 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001536
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001537 txn = smp->strm->txn;
Christopher Faulet09f88362021-04-01 16:00:29 +02001538 if (!txn || txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001539 return 0;
1540
1541 if (txn->req.flags & HTTP_MSGF_VER_11)
1542 smp->data.u.str.area = "HTTP/1.1";
1543 else
1544 smp->data.u.str.area = "HTTP/1.0";
1545
1546 smp->data.u.str.data = 8;
1547 smp->data.type = SMP_T_STR;
1548 smp->flags = SMP_F_CONST;
1549 return 1;
1550
1551}
1552
1553/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1554 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1555 */
1556static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1557{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001558 struct http_txn *txn;
1559
1560 if (!smp->strm)
1561 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001562
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001563 txn = smp->strm->txn;
Christopher Faulet09f88362021-04-01 16:00:29 +02001564 if (!txn || txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001565 return 0;
1566
1567 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1568 smp->data.u.str.area = "HTTP/1.1";
1569 else
1570 smp->data.u.str.area = "HTTP/1.0";
1571
1572 smp->data.u.str.data = 8;
1573 smp->data.type = SMP_T_STR;
1574 smp->flags = SMP_F_CONST;
1575 return 1;
1576
1577}
1578
1579/* Iterate over all cookies present in a message. The context is stored in
1580 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1581 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1582 * the direction, multiple cookies may be parsed on the same line or not.
Maciej Zdebdea7c202020-11-13 09:38:06 +00001583 * If provided, the searched cookie name is in args, in args->data.str. If
1584 * the input options indicate that no iterating is desired, then only last
1585 * value is fetched if any. If no cookie name is provided, the first cookie
1586 * value found is fetched. The returned sample is of type CSTR. Can be used
1587 * to parse cookies in other files.
Willy Tarreau79e57332018-10-02 16:01:16 +02001588 */
1589static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1590{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001591 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1592 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001593 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001594 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001595 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1596 struct ist hdr;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001597 char *cook = NULL;
1598 size_t cook_l = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001599 int found = 0;
1600
Christopher Faulet623af932021-01-29 11:22:15 +01001601 if (args->type == ARGT_STR) {
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001602 cook = args->data.str.area;
1603 cook_l = args->data.str.data;
1604 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001605
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001606 if (!ctx) {
1607 /* first call */
1608 ctx = &static_http_hdr_ctx;
1609 ctx->blk = NULL;
1610 smp->ctx.a[2] = ctx;
1611 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001612
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001613 if (!htx)
1614 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001615
Christopher Faulet16032ab2020-04-30 11:30:00 +02001616 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001617
Maciej Zdebdea7c202020-11-13 09:38:06 +00001618 /* OK so basically here, either we want only one value or we want to
1619 * iterate over all of them and we fetch the next one. In this last case
1620 * SMP_OPT_ITERATE option is set.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001621 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001622
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001623 if (!(smp->flags & SMP_F_NOT_LAST)) {
1624 /* search for the header from the beginning, we must first initialize
1625 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001626 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001627 smp->ctx.a[0] = NULL;
1628 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001629 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001630
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001631 smp->flags |= SMP_F_VOL_HDR;
1632 while (1) {
1633 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1634 if (!smp->ctx.a[0]) {
1635 if (!http_find_header(htx, hdr, ctx, 0))
1636 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001637
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001638 if (ctx->value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001639 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001640
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001641 smp->ctx.a[0] = ctx->value.ptr;
1642 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001643 }
1644
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001645 smp->data.type = SMP_T_STR;
1646 smp->flags |= SMP_F_CONST;
1647 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001648 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001649 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1650 &smp->data.u.str.area,
1651 &smp->data.u.str.data);
1652 if (smp->ctx.a[0]) {
1653 found = 1;
Maciej Zdebdea7c202020-11-13 09:38:06 +00001654 if (smp->opt & SMP_OPT_ITERATE) {
1655 /* iterate on cookie value */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001656 smp->flags |= SMP_F_NOT_LAST;
1657 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001658 }
Maciej Zdebdea7c202020-11-13 09:38:06 +00001659 if (args->data.str.data == 0) {
1660 /* No cookie name, first occurrence returned */
1661 break;
1662 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001663 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001664 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001665 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001666
Willy Tarreau79e57332018-10-02 16:01:16 +02001667 /* all cookie headers and values were scanned. If we're looking for the
1668 * last occurrence, we may return it now.
1669 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001670 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001671 smp->flags &= ~SMP_F_NOT_LAST;
1672 return found;
1673}
1674
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001675/* Same than smp_fetch_cookie() but only relies on the sample direction to
1676 * choose the right channel. So instead of duplicating the code, we just change
1677 * the keyword and then fallback on smp_fetch_cookie().
1678 */
1679static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1680{
1681 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1682 return smp_fetch_cookie(args, smp, kw, private);
1683}
1684
Willy Tarreau79e57332018-10-02 16:01:16 +02001685/* Iterate over all cookies present in a request to count how many occurrences
1686 * match the name in args and args->data.str.len. If <multi> is non-null, then
1687 * multiple cookies may be parsed on the same line. The returned sample is of
1688 * type UINT. Accepts exactly 1 argument of type string.
1689 */
1690static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1691{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001692 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1693 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001694 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001695 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001696 struct http_hdr_ctx ctx;
1697 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001698 char *val_beg, *val_end;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001699 char *cook = NULL;
1700 size_t cook_l = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001701 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001702
Christopher Faulet623af932021-01-29 11:22:15 +01001703 if (args->type == ARGT_STR){
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001704 cook = args->data.str.area;
1705 cook_l = args->data.str.data;
1706 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001707
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001708 if (!htx)
1709 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001710
Christopher Faulet16032ab2020-04-30 11:30:00 +02001711 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001712
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001713 val_end = val_beg = NULL;
1714 ctx.blk = NULL;
1715 cnt = 0;
1716 while (1) {
1717 /* Note: val_beg == NULL every time we need to fetch a new header */
1718 if (!val_beg) {
1719 if (!http_find_header(htx, hdr, &ctx, 0))
1720 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001721
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001722 if (ctx.value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001723 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001724
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001725 val_beg = ctx.value.ptr;
1726 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001727 }
1728
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001729 smp->data.type = SMP_T_STR;
1730 smp->flags |= SMP_F_CONST;
1731 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001732 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001733 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1734 &smp->data.u.str.area,
1735 &smp->data.u.str.data))) {
1736 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001737 }
1738 }
1739
1740 smp->data.type = SMP_T_SINT;
1741 smp->data.u.sint = cnt;
1742 smp->flags |= SMP_F_VOL_HDR;
1743 return 1;
1744}
1745
1746/* Fetch an cookie's integer value. The integer value is returned. It
1747 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1748 */
1749static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1750{
1751 int ret = smp_fetch_cookie(args, smp, kw, private);
1752
1753 if (ret > 0) {
1754 smp->data.type = SMP_T_SINT;
1755 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1756 smp->data.u.str.data);
1757 }
1758
1759 return ret;
1760}
1761
1762/************************************************************************/
1763/* The code below is dedicated to sample fetches */
1764/************************************************************************/
1765
1766/* This scans a URL-encoded query string. It takes an optionally wrapping
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001767 * string whose first contiguous chunk has its beginning in ctx->a[0] and end
Willy Tarreau79e57332018-10-02 16:01:16 +02001768 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1769 * pointers are updated for next iteration before leaving.
1770 */
1771static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1772{
1773 const char *vstart, *vend;
1774 struct buffer *temp;
1775 const char **chunks = (const char **)smp->ctx.a;
1776
1777 if (!http_find_next_url_param(chunks, name, name_len,
1778 &vstart, &vend, delim))
1779 return 0;
1780
1781 /* Create sample. If the value is contiguous, return the pointer as CONST,
1782 * if the value is wrapped, copy-it in a buffer.
1783 */
1784 smp->data.type = SMP_T_STR;
1785 if (chunks[2] &&
1786 vstart >= chunks[0] && vstart <= chunks[1] &&
1787 vend >= chunks[2] && vend <= chunks[3]) {
1788 /* Wrapped case. */
1789 temp = get_trash_chunk();
1790 memcpy(temp->area, vstart, chunks[1] - vstart);
1791 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1792 vend - chunks[2]);
1793 smp->data.u.str.area = temp->area;
1794 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1795 } else {
1796 /* Contiguous case. */
1797 smp->data.u.str.area = (char *)vstart;
1798 smp->data.u.str.data = vend - vstart;
1799 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1800 }
1801
1802 /* Update context, check wrapping. */
1803 chunks[0] = vend;
1804 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1805 chunks[1] = chunks[3];
1806 chunks[2] = NULL;
1807 }
1808
1809 if (chunks[0] < chunks[1])
1810 smp->flags |= SMP_F_NOT_LAST;
1811
1812 return 1;
1813}
1814
1815/* This function iterates over each parameter of the query string. It uses
1816 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1817 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1818 * An optional parameter name is passed in args[0], otherwise any parameter is
1819 * considered. It supports an optional delimiter argument for the beginning of
1820 * the string in args[1], which defaults to "?".
1821 */
1822static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1823{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001824 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001825 char delim = '?';
1826 const char *name;
1827 int name_len;
1828
Christopher Faulet623af932021-01-29 11:22:15 +01001829 if ((args[0].type && args[0].type != ARGT_STR) ||
Willy Tarreau79e57332018-10-02 16:01:16 +02001830 (args[1].type && args[1].type != ARGT_STR))
1831 return 0;
1832
1833 name = "";
1834 name_len = 0;
1835 if (args->type == ARGT_STR) {
1836 name = args->data.str.area;
1837 name_len = args->data.str.data;
1838 }
1839
1840 if (args[1].type)
1841 delim = *args[1].data.str.area;
1842
1843 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001844 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001845 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001846
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001847 if (!htx)
1848 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001849
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001850 sl = http_get_stline(htx);
1851 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1852 if (!smp->ctx.a[0])
1853 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001854
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001855 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001856
1857 /* Assume that the context is filled with NULL pointer
1858 * before the first call.
1859 * smp->ctx.a[2] = NULL;
1860 * smp->ctx.a[3] = NULL;
1861 */
1862 }
1863
1864 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1865}
1866
1867/* This function iterates over each parameter of the body. This requires
1868 * that the body has been waited for using http-buffer-request. It uses
1869 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001870 * contiguous part of the body, and optionally ctx->a[2..3] to reference the
Willy Tarreau79e57332018-10-02 16:01:16 +02001871 * optional second part if the body wraps at the end of the buffer. An optional
1872 * parameter name is passed in args[0], otherwise any parameter is considered.
1873 */
1874static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1875{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001876 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001877 const char *name;
1878 int name_len;
1879
Christopher Faulet623af932021-01-29 11:22:15 +01001880 if (args[0].type && args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001881 return 0;
1882
1883 name = "";
1884 name_len = 0;
1885 if (args[0].type == ARGT_STR) {
1886 name = args[0].data.str.area;
1887 name_len = args[0].data.str.data;
1888 }
1889
1890 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulete596d182020-05-05 17:46:34 +02001891 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001892 struct buffer *temp;
1893 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001894
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001895 if (!htx)
1896 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001897
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001898 temp = get_trash_chunk();
1899 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1900 struct htx_blk *blk = htx_get_blk(htx, pos);
1901 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001902
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001903 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001904 break;
1905 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001906 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001907 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001908 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001909 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001910
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001911 smp->ctx.a[0] = temp->area;
1912 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001913
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001914 /* Assume that the context is filled with NULL pointer
1915 * before the first call.
1916 * smp->ctx.a[2] = NULL;
1917 * smp->ctx.a[3] = NULL;
1918 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001919
Willy Tarreau79e57332018-10-02 16:01:16 +02001920 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001921
Willy Tarreau79e57332018-10-02 16:01:16 +02001922 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1923}
1924
1925/* Return the signed integer value for the specified url parameter (see url_param
1926 * above).
1927 */
1928static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1929{
1930 int ret = smp_fetch_url_param(args, smp, kw, private);
1931
1932 if (ret > 0) {
1933 smp->data.type = SMP_T_SINT;
1934 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1935 smp->data.u.str.data);
1936 }
1937
1938 return ret;
1939}
1940
1941/* This produces a 32-bit hash of the concatenation of the first occurrence of
1942 * the Host header followed by the path component if it begins with a slash ('/').
1943 * This means that '*' will not be added, resulting in exactly the first Host
1944 * entry. If no Host header is found, then the path is used. The resulting value
1945 * is hashed using the url hash followed by a full avalanche hash and provides a
1946 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1947 * high-traffic sites without having to store whole paths.
1948 * this differs from the base32 functions in that it includes the url parameters
1949 * as well as the path
1950 */
1951static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1952{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001953 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001954 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001955 struct http_hdr_ctx ctx;
1956 struct htx_sl *sl;
1957 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001958 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001959
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001960 if (!htx)
1961 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001962
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001963 ctx.blk = NULL;
1964 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1965 /* OK we have the header value in ctx.value */
1966 while (ctx.value.len--)
1967 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001968 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001969
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001970 /* now retrieve the path */
1971 sl = http_get_stline(htx);
1972 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001973 if (path.len && *(path.ptr) == '/') {
1974 while (path.len--)
1975 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001976 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001977
Willy Tarreau79e57332018-10-02 16:01:16 +02001978 hash = full_hash(hash);
1979
1980 smp->data.type = SMP_T_SINT;
1981 smp->data.u.sint = hash;
1982 smp->flags = SMP_F_VOL_1ST;
1983 return 1;
1984}
1985
1986/* This concatenates the source address with the 32-bit hash of the Host and
1987 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1988 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1989 * on the source address length. The URL hash is stored before the address so
1990 * that in environments where IPv6 is insignificant, truncating the output to
1991 * 8 bytes would still work.
1992 */
1993static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1994{
1995 struct buffer *temp;
1996 struct connection *cli_conn = objt_conn(smp->sess->origin);
1997
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001998 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001999 return 0;
2000
2001 if (!smp_fetch_url32(args, smp, kw, private))
2002 return 0;
2003
2004 temp = get_trash_chunk();
2005 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2006 temp->data += sizeof(unsigned int);
2007
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002008 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02002009 case AF_INET:
2010 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002011 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002012 4);
2013 temp->data += 4;
2014 break;
2015 case AF_INET6:
2016 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002017 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002018 16);
2019 temp->data += 16;
2020 break;
2021 default:
2022 return 0;
2023 }
2024
2025 smp->data.u.str = *temp;
2026 smp->data.type = SMP_T_BIN;
2027 return 1;
2028}
2029
2030/************************************************************************/
2031/* Other utility functions */
2032/************************************************************************/
2033
2034/* This function is used to validate the arguments passed to any "hdr" fetch
2035 * keyword. These keywords support an optional positive or negative occurrence
2036 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2037 * is assumed that the types are already the correct ones. Returns 0 on error,
2038 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2039 * error message in case of error, that the caller is responsible for freeing.
2040 * The initial location must either be freeable or NULL.
2041 * Note: this function's pointer is checked from Lua.
2042 */
2043int val_hdr(struct arg *arg, char **err_msg)
2044{
2045 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2046 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2047 return 0;
2048 }
2049 return 1;
2050}
2051
2052/************************************************************************/
2053/* All supported sample fetch keywords must be declared here. */
2054/************************************************************************/
2055
2056/* Note: must not be declared <const> as its list will be overwritten */
2057static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2058 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2059 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2060 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Yves Lafonb4d37082021-02-11 11:01:28 +01002061 { "baseq", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002062
2063 /* capture are allocated and are permanent in the stream */
2064 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2065
2066 /* retrieve these captures from the HTTP logs */
2067 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2068 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2069 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2070
2071 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2072 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2073
2074 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2075 * are only here to match the ACL's name, are request-only and are used
2076 * for ACL compatibility only.
2077 */
2078 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002079 { "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 +02002080 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2081 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2082
2083 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2084 * only here to match the ACL's name, are request-only and are used for
2085 * ACL compatibility only.
2086 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002087 { "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 +02002088 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2089 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2090 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2091
Christopher Fauleta4063562019-08-02 11:51:37 +02002092 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2093 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2094 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002095 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2096 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2097 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2098 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2099 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Faulete720c322020-09-02 17:25:18 +02002100 { "pathq", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002101 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2102
2103 /* HTTP protocol on the request path */
2104 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2105 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2106
2107 /* HTTP version on the request path */
2108 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2109 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2110
2111 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2112 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2113 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2114 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2115
2116 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2117 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2118
2119 /* HTTP version on the response path */
2120 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2121 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2122
Christopher Faulete596d182020-05-05 17:46:34 +02002123 { "res.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2124 { "res.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2125 { "res.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2126
2127 { "res.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2128 { "res.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2129
Willy Tarreau79e57332018-10-02 16:01:16 +02002130 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2131 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2132 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2133 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2134
2135 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2136 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2137 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2138 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2139 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2140 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2141 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2142
2143 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2144 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2145 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2146 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2147
2148 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2149 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2150 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2151 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2152 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2153 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2154 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2155
2156 /* scook is valid only on the response and is used for ACL compatibility */
2157 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2158 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2159 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2160 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2161
2162 /* shdr is valid only on the response and is used for ACL compatibility */
2163 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2164 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2165 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2166 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2167
2168 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2169 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2170 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2171 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2172 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2173 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2174 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2175 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2176 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2177 { "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 +02002178
Willy Tarreau79e57332018-10-02 16:01:16 +02002179 { /* END */ },
2180}};
2181
Willy Tarreau0108d902018-11-25 19:14:37 +01002182INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002183
2184/*
2185 * Local variables:
2186 * c-indent-level: 8
2187 * c-basic-offset: 8
2188 * End:
2189 */