blob: 798f7f6dff0e4c243e230a0075ed9f085c5756c7 [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
2 * HTTP samples fetching
3 *
4 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/types.h>
14
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
18
Willy Tarreaub2551052020-06-09 09:07:15 +020019#include <haproxy/api.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020020#include <haproxy/arg.h>
Willy Tarreauac13aea2020-06-04 10:36:03 +020021#include <haproxy/auth.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020022#include <haproxy/base64.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020023#include <haproxy/channel.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020024#include <haproxy/chunk.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020025#include <haproxy/connection.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020026#include <haproxy/global.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020027#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020028#include <haproxy/h1_htx.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020029#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020030#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020031#include <haproxy/http_fetch.h>
Willy Tarreau87735332020-06-04 09:08:41 +020032#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020033#include <haproxy/htx.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020034#include <haproxy/obj_type.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020035#include <haproxy/pool.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020036#include <haproxy/sample.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/stream.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020038#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020039#include <haproxy/version.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020040
Willy Tarreau79e57332018-10-02 16:01:16 +020041
42/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Fauletef453ed2018-10-24 21:39:27 +020043static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
Richard Russo458eafb2019-07-31 11:45:56 -070044/* this is used to convert raw connection buffers to htx */
45static THREAD_LOCAL struct buffer static_raw_htx_chunk;
46static THREAD_LOCAL char *static_raw_htx_buf;
Christopher Fauletef453ed2018-10-24 21:39:27 +020047
Christopher Faulet89dc4992019-04-17 12:02:59 +020048#define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL)
49#define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL)
Willy Tarreau79e57332018-10-02 16:01:16 +020050
Richard Russo458eafb2019-07-31 11:45:56 -070051/* This function returns the static htx chunk, where raw connections get
52 * converted to HTX as needed for samplxsing.
53 */
54struct buffer *get_raw_htx_chunk(void)
55{
56 chunk_reset(&static_raw_htx_chunk);
57 return &static_raw_htx_chunk;
58}
59
60static int alloc_raw_htx_chunk_per_thread()
61{
62 static_raw_htx_buf = malloc(global.tune.bufsize);
63 if (!static_raw_htx_buf)
64 return 0;
65 chunk_init(&static_raw_htx_chunk, static_raw_htx_buf, global.tune.bufsize);
66 return 1;
67}
68
69static void free_raw_htx_chunk_per_thread()
70{
Willy Tarreau61cfdf42021-02-20 10:46:51 +010071 ha_free(&static_raw_htx_buf);
Richard Russo458eafb2019-07-31 11:45:56 -070072}
73
74REGISTER_PER_THREAD_ALLOC(alloc_raw_htx_chunk_per_thread);
75REGISTER_PER_THREAD_FREE(free_raw_htx_chunk_per_thread);
76
Willy Tarreau79e57332018-10-02 16:01:16 +020077/*
78 * Returns the data from Authorization header. Function may be called more
79 * than once so data is stored in txn->auth_data. When no header is found
80 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
81 * searching again for something we are unable to find anyway. However, if
82 * the result if valid, the cache is not reused because we would risk to
83 * have the credentials overwritten by another stream in parallel.
Willy Tarreaueae83722020-04-29 11:52:51 +020084 * The caller is responsible for passing a sample with a valid stream/txn,
85 * and a valid htx.
Willy Tarreau79e57332018-10-02 16:01:16 +020086 */
87
Christopher Fauletcd761952019-07-15 13:58:29 +020088static int get_http_auth(struct sample *smp, struct htx *htx)
Willy Tarreau79e57332018-10-02 16:01:16 +020089{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020090 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020091 struct http_txn *txn = s->txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020092 struct http_hdr_ctx ctx = { .blk = NULL };
93 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020094 struct buffer auth_method;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020095 char *p;
Willy Tarreau79e57332018-10-02 16:01:16 +020096 int len;
97
98#ifdef DEBUG_AUTH
99 printf("Auth for stream %p: %d\n", s, txn->auth.method);
100#endif
Willy Tarreau79e57332018-10-02 16:01:16 +0200101 if (txn->auth.method == HTTP_AUTH_WRONG)
102 return 0;
103
104 txn->auth.method = HTTP_AUTH_WRONG;
105
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200106 if (txn->flags & TX_USE_PX_CONN)
107 hdr = ist("Proxy-Authorization");
108 else
109 hdr = ist("Authorization");
Willy Tarreau79e57332018-10-02 16:01:16 +0200110
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200111 ctx.blk = NULL;
112 if (!http_find_header(htx, hdr, &ctx, 0))
113 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200114
Willy Tarreau17254932020-09-02 07:08:47 +0200115 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
116 if (!p || p == ctx.value.ptr) /* if no space was found or if the space is the first character */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200117 return 0;
Willy Tarreau17254932020-09-02 07:08:47 +0200118 len = p - ctx.value.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +0200119
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200120 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
121 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200122
Remi Tricot-Le Breton2ad2ed42021-10-29 15:25:18 +0200123 /* According to RFC7235, there could be multiple spaces between the
124 * scheme and its value, we must skip all of them.
125 */
126 while (p < istend(ctx.value) && *p == ' ')
127 ++p;
128
129 chunk_initlen(&txn->auth.method_data, p, 0, istend(ctx.value) - p);
Willy Tarreau79e57332018-10-02 16:01:16 +0200130
131 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
132 struct buffer *http_auth = get_trash_chunk();
133
134 len = base64dec(txn->auth.method_data.area,
135 txn->auth.method_data.data,
136 http_auth->area, global.tune.bufsize - 1);
137
138 if (len < 0)
139 return 0;
140
141
142 http_auth->area[len] = '\0';
143
144 p = strchr(http_auth->area, ':');
145
146 if (!p)
147 return 0;
148
149 txn->auth.user = http_auth->area;
150 *p = '\0';
151 txn->auth.pass = p+1;
152
153 txn->auth.method = HTTP_AUTH_BASIC;
154 return 1;
155 }
156
157 return 0;
158}
159
160/* This function ensures that the prerequisites for an L7 fetch are ready,
161 * which means that a request or response is ready. If some data is missing,
162 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200163 * to extract data from L7. If <vol> is non-null during a prefetch, another
164 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200165 *
166 * The function returns :
167 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
168 * decide whether or not an HTTP message is present ;
169 * NULL if the requested data cannot be fetched or if it is certain that
Willy Tarreaueae83722020-04-29 11:52:51 +0200170 * we'll never have any HTTP message there; this includes null strm or chn.
Willy Tarreaua6d98792020-08-12 14:04:52 +0200171 * NULL if the sample's direction does not match the channel's (i.e. the
172 * function was asked to work on the wrong channel)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200173 * The HTX message if ready
174 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200175struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, struct check *check, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200176{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200177 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200178 struct http_txn *txn = NULL;
179 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200180 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100181 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200182
Willy Tarreaua6d98792020-08-12 14:04:52 +0200183 if (chn &&
184 (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ && (chn->flags & CF_ISRESP)) ||
185 ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES && !(chn->flags & CF_ISRESP))))
186 return 0;
187
Christopher Fauletef453ed2018-10-24 21:39:27 +0200188 /* Note: it is possible that <s> is NULL when called before stream
189 * initialization (eg: tcp-request connection), so this function is the
190 * one responsible for guarding against this case for all HTTP users.
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200191 *
192 * In the health check context, the stream and the channel must be NULL
193 * and <check> must be set. In this case, only the input buffer,
194 * corresponding to the response, is considered. It is the caller
195 * responsibility to provide <check>.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200196 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200197 BUG_ON(check && (s || chn));
198 if (!s || !chn) {
199 if (check) {
200 htx = htxbuf(&check->bi);
201
202 /* Analyse not yet started */
203 if (htx_is_empty(htx) || htx->first == -1)
204 return NULL;
205
206 sl = http_get_stline(htx);
207 if (vol && !sl) {
208 /* The start-line was already forwarded, it is too late to fetch anything */
209 return NULL;
210 }
211 goto end;
212 }
213
Christopher Fauletef453ed2018-10-24 21:39:27 +0200214 return NULL;
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200215 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200216
Christopher Faulet75f619a2021-03-08 19:12:58 +0100217 if (!s->txn && !http_create_txn(s))
218 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200219 txn = s->txn;
220 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200221
Christopher Fauleteca88542019-04-03 10:12:42 +0200222 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200223 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200224
Christopher Faulet89dc4992019-04-17 12:02:59 +0200225 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
226 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200227
Christopher Faulet89dc4992019-04-17 12:02:59 +0200228 if (msg->msg_state < HTTP_MSG_BODY) {
229 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200230 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200231 /* Parsing is done by the mux, just wait */
232 smp->flags |= SMP_F_MAY_CHANGE;
233 return NULL;
234 }
235 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200236 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200237 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200238 /* The start-line was already forwarded, it is too late to fetch anything */
239 return NULL;
240 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200241 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200242 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200243 struct buffer *buf;
244 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200245 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200246 union h1_sl h1sl;
247 unsigned int flags = HTX_FL_NONE;
248 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200249
Christopher Faulet89dc4992019-04-17 12:02:59 +0200250 /* no HTTP fetch on the response in TCP mode */
251 if (chn->flags & CF_ISRESP)
252 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200253
Christopher Faulet89dc4992019-04-17 12:02:59 +0200254 /* Now we are working on the request only */
255 buf = &chn->buf;
256 if (b_head(buf) + b_data(buf) > b_wrap(buf))
257 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200258
Christopher Faulet89dc4992019-04-17 12:02:59 +0200259 h1m_init_req(&h1m);
260 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
261 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
262 if (ret <= 0) {
263 /* Invalid or too big*/
264 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200265 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100266
Christopher Faulet89dc4992019-04-17 12:02:59 +0200267 /* wait for a full request */
268 smp->flags |= SMP_F_MAY_CHANGE;
269 return NULL;
270 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100271
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500272 /* OK we just got a valid HTTP message. We have to convert it
Christopher Faulet89dc4992019-04-17 12:02:59 +0200273 * into an HTX message.
274 */
275 if (unlikely(h1sl.rq.v.len == 0)) {
276 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
277 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200278 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200279 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200280 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200281
282 /* Set HTX start-line flags */
283 if (h1m.flags & H1_MF_VER_11)
284 flags |= HTX_SL_F_VER_11;
285 if (h1m.flags & H1_MF_XFER_ENC)
286 flags |= HTX_SL_F_XFER_ENC;
287 flags |= HTX_SL_F_XFER_LEN;
288 if (h1m.flags & H1_MF_CHNK)
289 flags |= HTX_SL_F_CHNK;
290 else if (h1m.flags & H1_MF_CLEN)
291 flags |= HTX_SL_F_CLEN;
292
Richard Russo458eafb2019-07-31 11:45:56 -0700293 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200294 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
295 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200296 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200297 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200298 }
299
300 /* OK we just got a valid HTTP message. If not already done by
301 * HTTP analyzers, we have some minor preparation to perform so
302 * that further checks can rely on HTTP tests.
303 */
304 if (sl && msg->msg_state < HTTP_MSG_BODY) {
305 if (!(chn->flags & CF_ISRESP)) {
306 txn->meth = sl->info.req.meth;
307 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
308 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200309 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200310 else
311 txn->status = sl->info.res.status;
312 if (sl->flags & HTX_SL_F_VER_11)
313 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200314 }
315
316 /* everything's OK */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200317 end:
Christopher Fauletef453ed2018-10-24 21:39:27 +0200318 return htx;
319}
320
Willy Tarreau79e57332018-10-02 16:01:16 +0200321/* This function fetches the method of current HTTP request and stores
322 * it in the global pattern struct as a chunk. There are two possibilities :
323 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
324 * in <len> and <ptr> is NULL ;
325 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
326 * <len> to its length.
327 * This is intended to be used with pat_match_meth() only.
328 */
329static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
330{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200331 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200332 struct http_txn *txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200333 int meth;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200334
Willy Tarreaua6d98792020-08-12 14:04:52 +0200335 txn = smp->strm->txn;
336 if (!txn)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200337 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200338
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200339 meth = txn->meth;
340 smp->data.type = SMP_T_METH;
341 smp->data.u.meth.meth = meth;
342 if (meth == HTTP_METH_OTHER) {
Willy Tarreaua6d98792020-08-12 14:04:52 +0200343 struct htx *htx;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200344 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200345
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200346 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
347 /* ensure the indexes are not affected */
348 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200349 }
Willy Tarreaua6d98792020-08-12 14:04:52 +0200350
Christopher Faulet6f97a612021-04-15 09:28:02 +0200351 htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreaua6d98792020-08-12 14:04:52 +0200352 if (!htx)
353 return 0;
354
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200355 sl = http_get_stline(htx);
356 smp->flags |= SMP_F_CONST;
357 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
358 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200359 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200360 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200361 return 1;
362}
363
364static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
365{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200366 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200367 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200368 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200369 char *ptr;
370 int len;
371
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200372 if (!htx)
373 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200374
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200375 sl = http_get_stline(htx);
376 len = HTX_SL_REQ_VLEN(sl);
377 ptr = HTX_SL_REQ_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200378
379 while ((len-- > 0) && (*ptr++ != '/'));
380 if (len <= 0)
381 return 0;
382
383 smp->data.type = SMP_T_STR;
384 smp->data.u.str.area = ptr;
385 smp->data.u.str.data = len;
386
387 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
388 return 1;
389}
390
391static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
392{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200393 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200394 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200395 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200396 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200397 char *ptr;
398 int len;
399
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200400 if (!htx)
401 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200402
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200403 sl = http_get_stline(htx);
404 len = HTX_SL_RES_VLEN(sl);
405 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200406
407 while ((len-- > 0) && (*ptr++ != '/'));
408 if (len <= 0)
409 return 0;
410
411 smp->data.type = SMP_T_STR;
412 smp->data.u.str.area = ptr;
413 smp->data.u.str.data = len;
414
415 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
416 return 1;
417}
418
419/* 3. Check on Status Code. We manipulate integers here. */
420static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
421{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200422 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200423 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200424 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200425 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200426 char *ptr;
427 int len;
428
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200429 if (!htx)
430 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200431
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200432 sl = http_get_stline(htx);
433 len = HTX_SL_RES_CLEN(sl);
434 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200435
436 smp->data.type = SMP_T_SINT;
437 smp->data.u.sint = __strl2ui(ptr, len);
438 smp->flags = SMP_F_VOL_1ST;
439 return 1;
440}
441
442static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
443{
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100444 struct ist unique_id;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100445
Willy Tarreau79e57332018-10-02 16:01:16 +0200446 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
447 return 0;
448
Willy Tarreaua1062a42020-04-29 11:50:38 +0200449 if (!smp->strm)
450 return 0;
451
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100452 unique_id = stream_generate_unique_id(smp->strm, &smp->sess->fe->format_unique_id);
453 if (!isttest(unique_id))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100454 return 0;
455
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100456 smp->data.u.str.area = smp->strm->unique_id.ptr;
457 smp->data.u.str.data = smp->strm->unique_id.len;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100458 smp->data.type = SMP_T_STR;
Willy Tarreau79e57332018-10-02 16:01:16 +0200459 smp->flags = SMP_F_CONST;
460 return 1;
461}
462
463/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800464 * empty line which separes headers from the body. This is useful
465 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200466 */
467static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
468{
Christopher Faulete596d182020-05-05 17:46:34 +0200469 /* possible keywords: req.hdrs, res.hdrs */
470 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200471 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200472 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200473 struct buffer *temp;
474 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200475
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200476 if (!htx)
477 return 0;
478 temp = get_trash_chunk();
479 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
480 struct htx_blk *blk = htx_get_blk(htx, pos);
481 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200482
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200483 if (type == HTX_BLK_HDR) {
484 struct ist n = htx_get_blk_name(htx, blk);
485 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200486
Christopher Faulet53a899b2019-10-08 16:38:42 +0200487 if (!h1_format_htx_hdr(n, v, temp))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200488 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200489 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200490 else if (type == HTX_BLK_EOH) {
491 if (!chunk_memcat(temp, "\r\n", 2))
492 return 0;
493 break;
494 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200495 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200496 smp->data.type = SMP_T_STR;
497 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200498 return 1;
499}
500
501/* Returns the header request in a length/value encoded format.
502 * This is useful for exchanges with the SPOE.
503 *
504 * A "length value" is a multibyte code encoding numbers. It uses the
505 * SPOE format. The encoding is the following:
506 *
507 * Each couple "header name" / "header value" is composed
508 * like this:
509 * "length value" "header name bytes"
510 * "length value" "header value bytes"
511 * When the last header is reached, the header name and the header
512 * value are empty. Their length are 0
513 */
514static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
515{
Christopher Faulete596d182020-05-05 17:46:34 +0200516 /* possible keywords: req.hdrs_bin, res.hdrs_bin */
517 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200518 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200519 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200520 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200521 char *p, *end;
522 int32_t pos;
523 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200524
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200525 if (!htx)
526 return 0;
527 temp = get_trash_chunk();
528 p = temp->area;
529 end = temp->area + temp->size;
530 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
531 struct htx_blk *blk = htx_get_blk(htx, pos);
532 enum htx_blk_type type = htx_get_blk_type(blk);
533 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200534
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200535 if (type == HTX_BLK_HDR) {
536 n = htx_get_blk_name(htx,blk);
537 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200538
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200539 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200540 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200541 if (ret == -1)
542 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200543 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200544 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200545 memcpy(p, n.ptr, n.len);
546 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200547
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200548 /* encode the header value. */
549 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200550 if (ret == -1)
551 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200552 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200553 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200554 memcpy(p, v.ptr, v.len);
555 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200556
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200557 }
558 else if (type == HTX_BLK_EOH) {
559 /* encode the end of the header list with empty
560 * header name and header value.
561 */
562 ret = encode_varint(0, &p, end);
563 if (ret == -1)
564 return 0;
565 ret = encode_varint(0, &p, end);
566 if (ret == -1)
567 return 0;
568 break;
569 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200570 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200571
572 /* Initialise sample data which will be filled. */
573 smp->data.type = SMP_T_BIN;
574 smp->data.u.str.area = temp->area;
575 smp->data.u.str.data = p - temp->area;
576 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200577 return 1;
578}
579
580/* returns the longest available part of the body. This requires that the body
581 * has been waited for using http-buffer-request.
582 */
583static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
584{
Christopher Faulete596d182020-05-05 17:46:34 +0200585 /* possible keywords: req.body, res.body */
586 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200587 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200588 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200589 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200590 int32_t pos;
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100591 int finished = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200592
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200593 if (!htx)
594 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200595
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200596 temp = get_trash_chunk();
597 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
598 struct htx_blk *blk = htx_get_blk(htx, pos);
599 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200600
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100601 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) {
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100602 finished = 1;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200603 break;
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100604 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200605 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +0200606 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200607 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200608 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200609 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200610
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200611 smp->data.type = SMP_T_BIN;
612 smp->data.u.str = *temp;
613 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200614
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100615 if (!finished && (check || (chn && !channel_full(chn, global.tune.maxrewrite) &&
616 !(chn->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)))))
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200617 smp->flags |= SMP_F_MAY_CHANGE;
618
Willy Tarreau79e57332018-10-02 16:01:16 +0200619 return 1;
620}
621
622
623/* returns the available length of the body. This requires that the body
624 * has been waited for using http-buffer-request.
625 */
626static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
627{
Christopher Faulete596d182020-05-05 17:46:34 +0200628 /* possible keywords: req.body_len, res.body_len */
629 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200630 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200631 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200632 int32_t pos;
633 unsigned long long len = 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100634
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200635 if (!htx)
636 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100637
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200638 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
639 struct htx_blk *blk = htx_get_blk(htx, pos);
640 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100641
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100642 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200643 break;
644 if (type == HTX_BLK_DATA)
645 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200646 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200647
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200648 smp->data.type = SMP_T_SINT;
649 smp->data.u.sint = len;
650 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200651 return 1;
652}
653
654
655/* returns the advertised length of the body, or the advertised size of the
656 * chunks available in the buffer. This requires that the body has been waited
657 * for using http-buffer-request.
658 */
659static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
660{
Christopher Faulete596d182020-05-05 17:46:34 +0200661 /* possible keywords: req.body_size, res.body_size */
662 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200663 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200664 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200665 int32_t pos;
666 unsigned long long len = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200667
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200668 if (!htx)
669 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100670
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200671 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
672 struct htx_blk *blk = htx_get_blk(htx, pos);
673 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100674
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100675 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200676 break;
677 if (type == HTX_BLK_DATA)
678 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200679 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200680 if (htx->extra != ULLONG_MAX)
681 len += htx->extra;
Willy Tarreau79e57332018-10-02 16:01:16 +0200682
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200683 smp->data.type = SMP_T_SINT;
684 smp->data.u.sint = len;
685 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200686 return 1;
687}
688
689
690/* 4. Check on URL/URI. A pointer to the URI is stored. */
691static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
692{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200693 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200694 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200695 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200696
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200697 if (!htx)
698 return 0;
699 sl = http_get_stline(htx);
700 smp->data.type = SMP_T_STR;
701 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
702 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
703 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200704 return 1;
705}
706
707static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
708{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200709 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200710 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200711 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200712 struct sockaddr_storage addr;
713
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200714 memset(&addr, 0, sizeof(addr));
715
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200716 if (!htx)
717 return 0;
718 sl = http_get_stline(htx);
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200719 if (url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL) < 0)
720 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200721
Willy Tarreau48584642021-05-09 10:32:54 +0200722 if (addr.ss_family != AF_INET)
Willy Tarreau79e57332018-10-02 16:01:16 +0200723 return 0;
724
725 smp->data.type = SMP_T_IPV4;
726 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
727 smp->flags = 0;
728 return 1;
729}
730
731static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
732{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200733 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200734 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200735 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200736 struct sockaddr_storage addr;
737
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200738 memset(&addr, 0, sizeof(addr));
739
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200740 if (!htx)
741 return 0;
742 sl = http_get_stline(htx);
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200743 if (url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL) < 0)
744 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200745
Willy Tarreau48584642021-05-09 10:32:54 +0200746 if (addr.ss_family != AF_INET)
Willy Tarreau79e57332018-10-02 16:01:16 +0200747 return 0;
748
749 smp->data.type = SMP_T_SINT;
Willy Tarreau48584642021-05-09 10:32:54 +0200750 smp->data.u.sint = get_host_port(&addr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200751 smp->flags = 0;
752 return 1;
753}
754
755/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
756 * Accepts an optional argument of type string containing the header field name,
757 * and an optional argument of type signed or unsigned integer to request an
758 * explicit occurrence of the header. Note that in the event of a missing name,
759 * headers are considered from the first one. It does not stop on commas and
760 * returns full lines instead (useful for User-Agent or Date for example).
761 */
762static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
763{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200764 /* possible keywords: req.fhdr, res.fhdr */
765 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200766 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200767 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200768 struct http_hdr_ctx *ctx = smp->ctx.a[0];
769 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200770 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200771
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200772 if (!ctx) {
773 /* first call */
774 ctx = &static_http_hdr_ctx;
775 ctx->blk = NULL;
776 smp->ctx.a[0] = ctx;
777 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200778
Christopher Faulet623af932021-01-29 11:22:15 +0100779 if (args[0].type != ARGT_STR)
780 return 0;
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100781 name = ist2(args[0].data.str.area, args[0].data.str.data);
Willy Tarreau79e57332018-10-02 16:01:16 +0200782
Christopher Faulet623af932021-01-29 11:22:15 +0100783 if (args[1].type == ARGT_SINT)
784 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200785
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200786 if (!htx)
787 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200788
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200789 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
790 /* search for header from the beginning */
791 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200792
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200793 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
794 /* no explicit occurrence and single fetch => last header by default */
795 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200796
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200797 if (!occ)
798 /* prepare to report multiple occurrences for ACL fetches */
799 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200800
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200801 smp->data.type = SMP_T_STR;
802 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
803 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
804 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200805 smp->flags &= ~SMP_F_NOT_LAST;
806 return 0;
807}
808
809/* 6. Check on HTTP header count. The number of occurrences is returned.
810 * Accepts exactly 1 argument of type string. It does not stop on commas and
811 * returns full lines instead (useful for User-Agent or Date for example).
812 */
813static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
814{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200815 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
816 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200817 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200818 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200819 struct http_hdr_ctx ctx;
820 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200821 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200822
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200823 if (!htx)
824 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200825
Christopher Faulet623af932021-01-29 11:22:15 +0100826 if (args->type == ARGT_STR) {
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100827 name = ist2(args->data.str.area, args->data.str.data);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200828 } else {
Tim Duesterhus68a088d2021-02-28 16:11:37 +0100829 name = IST_NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200830 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200831
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200832 ctx.blk = NULL;
833 cnt = 0;
834 while (http_find_header(htx, name, &ctx, 1))
835 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200836 smp->data.type = SMP_T_SINT;
837 smp->data.u.sint = cnt;
838 smp->flags = SMP_F_VOL_HDR;
839 return 1;
840}
841
842static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
843{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200844 /* possible keywords: req.hdr_names, res.hdr_names */
845 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200846 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200847 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200848 struct buffer *temp;
849 char del = ',';
850
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200851 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200852
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200853 if (!htx)
854 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200855
Christopher Faulet623af932021-01-29 11:22:15 +0100856 if (args->type == ARGT_STR)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200857 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200858
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200859 temp = get_trash_chunk();
860 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
861 struct htx_blk *blk = htx_get_blk(htx, pos);
862 enum htx_blk_type type = htx_get_blk_type(blk);
863 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200864
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200865 if (type == HTX_BLK_EOH)
866 break;
867 if (type != HTX_BLK_HDR)
868 continue;
869 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200870
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200871 if (temp->data)
872 temp->area[temp->data++] = del;
873 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200874 }
875
876 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200877 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200878 smp->flags = SMP_F_VOL_HDR;
879 return 1;
880}
881
882/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
883 * Accepts an optional argument of type string containing the header field name,
884 * and an optional argument of type signed or unsigned integer to request an
885 * explicit occurrence of the header. Note that in the event of a missing name,
886 * headers are considered from the first one.
887 */
888static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
889{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200890 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
891 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200892 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200893 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200894 struct http_hdr_ctx *ctx = smp->ctx.a[0];
895 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200896 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200897
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200898 if (!ctx) {
899 /* first call */
900 ctx = &static_http_hdr_ctx;
901 ctx->blk = NULL;
902 smp->ctx.a[0] = ctx;
903 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200904
Christopher Faulet623af932021-01-29 11:22:15 +0100905 if (args[0].type != ARGT_STR)
906 return 0;
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100907 name = ist2(args[0].data.str.area, args[0].data.str.data);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200908
Christopher Faulet623af932021-01-29 11:22:15 +0100909 if (args[1].type == ARGT_SINT)
910 occ = args[1].data.sint;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200911
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200912 if (!htx)
913 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200914
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200915 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
916 /* search for header from the beginning */
917 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200918
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200919 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
920 /* no explicit occurrence and single fetch => last header by default */
921 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200922
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200923 if (!occ)
924 /* prepare to report multiple occurrences for ACL fetches */
925 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200926
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200927 smp->data.type = SMP_T_STR;
928 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
929 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
930 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200931
932 smp->flags &= ~SMP_F_NOT_LAST;
933 return 0;
934}
935
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200936/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
937 * the right channel. So instead of duplicating the code, we just change the
938 * keyword and then fallback on smp_fetch_hdr().
939 */
940static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
941{
942 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
943 return smp_fetch_hdr(args, smp, kw, private);
944}
945
Willy Tarreau79e57332018-10-02 16:01:16 +0200946/* 6. Check on HTTP header count. The number of occurrences is returned.
947 * Accepts exactly 1 argument of type string.
948 */
949static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
950{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200951 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
952 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200953 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200954 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200955 struct http_hdr_ctx ctx;
956 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200957 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200958
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200959 if (!htx)
960 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200961
Christopher Faulet623af932021-01-29 11:22:15 +0100962 if (args->type == ARGT_STR) {
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100963 name = ist2(args->data.str.area, args->data.str.data);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200964 } else {
Tim Duesterhus68a088d2021-02-28 16:11:37 +0100965 name = IST_NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200966 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200967
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200968 ctx.blk = NULL;
969 cnt = 0;
970 while (http_find_header(htx, name, &ctx, 0))
971 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200972
973 smp->data.type = SMP_T_SINT;
974 smp->data.u.sint = cnt;
975 smp->flags = SMP_F_VOL_HDR;
976 return 1;
977}
978
979/* Fetch an HTTP header's integer value. The integer value is returned. It
980 * takes a mandatory argument of type string and an optional one of type int
981 * to designate a specific occurrence. It returns an unsigned integer, which
982 * may or may not be appropriate for everything.
983 */
984static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
985{
986 int ret = smp_fetch_hdr(args, smp, kw, private);
987
988 if (ret > 0) {
989 smp->data.type = SMP_T_SINT;
990 smp->data.u.sint = strl2ic(smp->data.u.str.area,
991 smp->data.u.str.data);
992 }
993
994 return ret;
995}
996
997/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
998 * and an optional one of type int to designate a specific occurrence.
Willy Tarreau7b0e00d2021-03-25 14:12:29 +0100999 * It returns an IPv4 or IPv6 address. Addresses surrounded by invalid chars
1000 * are rejected. However IPv4 addresses may be followed with a colon and a
1001 * valid port number.
Willy Tarreau79e57332018-10-02 16:01:16 +02001002 */
1003static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1004{
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001005 struct buffer *temp = get_trash_chunk();
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001006 int ret, len;
1007 int port;
Willy Tarreau79e57332018-10-02 16:01:16 +02001008
1009 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001010 if (smp->data.u.str.data < temp->size - 1) {
1011 memcpy(temp->area, smp->data.u.str.area,
1012 smp->data.u.str.data);
1013 temp->area[smp->data.u.str.data] = '\0';
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001014 len = url2ipv4((char *) temp->area, &smp->data.u.ipv4);
Willy Tarreau645dc082021-03-31 11:41:36 +02001015 if (len > 0 && len == smp->data.u.str.data) {
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001016 /* plain IPv4 address */
1017 smp->data.type = SMP_T_IPV4;
1018 break;
1019 } else if (len > 0 && temp->area[len] == ':' &&
1020 strl2irc(temp->area + len + 1, smp->data.u.str.data - len - 1, &port) == 0 &&
1021 port >= 0 && port <= 65535) {
1022 /* IPv4 address suffixed with ':' followed by a valid port number */
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001023 smp->data.type = SMP_T_IPV4;
1024 break;
1025 } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1026 smp->data.type = SMP_T_IPV6;
1027 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001028 }
1029 }
1030
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001031 /* if the header doesn't match an IP address, fetch next one */
1032 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001033 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001034 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001035 return ret;
1036}
Willy Tarreau79e57332018-10-02 16:01:16 +02001037
Christopher Faulete720c322020-09-02 17:25:18 +02001038/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at the
1039 * first '/' after the possible hostname. It ends before the possible '?' except
1040 * for 'pathq' keyword.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001041 */
1042static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1043{
1044 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001045 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001046 struct htx_sl *sl;
1047 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001048
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001049 if (!htx)
1050 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001051
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001052 sl = http_get_stline(htx);
Christopher Faulete720c322020-09-02 17:25:18 +02001053 path = http_get_path(htx_sl_req_uri(sl));
1054
Yves Lafonb4d37082021-02-11 11:01:28 +01001055 if (kw[4] == 'q' && (kw[0] == 'p' || kw[0] == 'b')) // pathq or baseq
Christopher Faulete720c322020-09-02 17:25:18 +02001056 path = http_get_path(htx_sl_req_uri(sl));
1057 else
1058 path = iststop(http_get_path(htx_sl_req_uri(sl)), '?');
1059
Tim Duesterhused526372020-03-05 17:56:33 +01001060 if (!isttest(path))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001061 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001062
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001063 /* OK, we got the '/' ! */
1064 smp->data.type = SMP_T_STR;
1065 smp->data.u.str.area = path.ptr;
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001066 smp->data.u.str.data = path.len;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001067 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001068 return 1;
1069}
1070
1071/* This produces a concatenation of the first occurrence of the Host header
1072 * followed by the path component if it begins with a slash ('/'). This means
1073 * that '*' will not be added, resulting in exactly the first Host entry.
1074 * If no Host header is found, then the path is returned as-is. The returned
1075 * value is stored in the trash so it does not need to be marked constant.
1076 * The returned sample is of type string.
1077 */
1078static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1079{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001080 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001081 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001082 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001083 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001084 struct http_hdr_ctx ctx;
1085 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001086
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001087 if (!htx)
1088 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001089
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001090 ctx.blk = NULL;
1091 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1092 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001093
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001094 /* OK we have the header value in ctx.value */
1095 temp = get_trash_chunk();
1096 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001097
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001098 /* now retrieve the path */
1099 sl = http_get_stline(htx);
1100 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001101 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001102 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001103
Yves Lafonb4d37082021-02-11 11:01:28 +01001104 if (kw[4] == 'q' && kw[0] == 'b') { // baseq
1105 len = path.len;
1106 } else {
1107 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1108 ;
1109 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001110
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001111 if (len && *(path.ptr) == '/')
1112 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001113 }
1114
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001115 smp->data.type = SMP_T_STR;
1116 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001117 smp->flags = SMP_F_VOL_1ST;
1118 return 1;
1119}
1120
1121/* This produces a 32-bit hash of the concatenation of the first occurrence of
1122 * the Host header followed by the path component if it begins with a slash ('/').
1123 * This means that '*' will not be added, resulting in exactly the first Host
1124 * entry. If no Host header is found, then the path is used. The resulting value
1125 * is hashed using the path hash followed by a full avalanche hash and provides a
1126 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1127 * high-traffic sites without having to store whole paths.
1128 */
1129static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1130{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001131 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001132 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001133 struct htx_sl *sl;
1134 struct http_hdr_ctx ctx;
1135 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001136 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001137
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001138 if (!htx)
1139 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001140
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001141 ctx.blk = NULL;
1142 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1143 /* OK we have the header value in ctx.value */
1144 while (ctx.value.len--)
1145 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001146 }
1147
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001148 /* now retrieve the path */
1149 sl = http_get_stline(htx);
1150 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001151 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001152 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001153
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001154 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1155 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001156
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001157 if (len && *(path.ptr) == '/') {
1158 while (len--)
1159 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001160 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001161 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001162
Willy Tarreau79e57332018-10-02 16:01:16 +02001163 hash = full_hash(hash);
1164
1165 smp->data.type = SMP_T_SINT;
1166 smp->data.u.sint = hash;
1167 smp->flags = SMP_F_VOL_1ST;
1168 return 1;
1169}
1170
1171/* This concatenates the source address with the 32-bit hash of the Host and
1172 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1173 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1174 * on the source address length. The path hash is stored before the address so
1175 * that in environments where IPv6 is insignificant, truncating the output to
1176 * 8 bytes would still work.
1177 */
1178static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1179{
1180 struct buffer *temp;
1181 struct connection *cli_conn = objt_conn(smp->sess->origin);
1182
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001183 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001184 return 0;
1185
1186 if (!smp_fetch_base32(args, smp, kw, private))
1187 return 0;
1188
1189 temp = get_trash_chunk();
1190 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1191 temp->data += sizeof(unsigned int);
1192
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001193 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001194 case AF_INET:
1195 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001196 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001197 4);
1198 temp->data += 4;
1199 break;
1200 case AF_INET6:
1201 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001202 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001203 16);
1204 temp->data += 16;
1205 break;
1206 default:
1207 return 0;
1208 }
1209
1210 smp->data.u.str = *temp;
1211 smp->data.type = SMP_T_BIN;
1212 return 1;
1213}
1214
1215/* Extracts the query string, which comes after the question mark '?'. If no
1216 * question mark is found, nothing is returned. Otherwise it returns a sample
1217 * of type string carrying the whole query string.
1218 */
1219static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1220{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001221 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001222 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001223 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001224 char *ptr, *end;
1225
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001226 if (!htx)
1227 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001228
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001229 sl = http_get_stline(htx);
1230 ptr = HTX_SL_REQ_UPTR(sl);
1231 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001232
1233 /* look up the '?' */
1234 do {
1235 if (ptr == end)
1236 return 0;
1237 } while (*ptr++ != '?');
1238
1239 smp->data.type = SMP_T_STR;
1240 smp->data.u.str.area = ptr;
1241 smp->data.u.str.data = end - ptr;
1242 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1243 return 1;
1244}
1245
1246static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1247{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001248 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001249 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001250
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001251 if (!htx)
1252 return 0;
1253 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001254 smp->data.u.sint = 1;
1255 return 1;
1256}
1257
1258/* return a valid test if the current request is the first one on the connection */
1259static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1260{
Willy Tarreau79512b62020-04-29 11:52:13 +02001261 if (!smp->strm)
1262 return 0;
1263
Willy Tarreau79e57332018-10-02 16:01:16 +02001264 smp->data.type = SMP_T_BOOL;
1265 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1266 return 1;
1267}
1268
Christopher Fauleta4063562019-08-02 11:51:37 +02001269/* Fetch the authentication method if there is an Authorization header. It
1270 * relies on get_http_auth()
1271 */
1272static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1273{
1274 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001275 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001276 struct http_txn *txn;
1277
1278 if (!htx)
1279 return 0;
1280
1281 txn = smp->strm->txn;
1282 if (!get_http_auth(smp, htx))
1283 return 0;
1284
1285 switch (txn->auth.method) {
1286 case HTTP_AUTH_BASIC:
1287 smp->data.u.str.area = "Basic";
1288 smp->data.u.str.data = 5;
1289 break;
1290 case HTTP_AUTH_DIGEST:
1291 /* Unexpected because not supported */
1292 smp->data.u.str.area = "Digest";
1293 smp->data.u.str.data = 6;
1294 break;
1295 default:
1296 return 0;
1297 }
1298
1299 smp->data.type = SMP_T_STR;
1300 smp->flags = SMP_F_CONST;
1301 return 1;
1302}
1303
1304/* Fetch the user supplied if there is an Authorization header. It relies on
1305 * get_http_auth()
1306 */
1307static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1308{
1309 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001310 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001311 struct http_txn *txn;
1312
1313 if (!htx)
1314 return 0;
1315
1316 txn = smp->strm->txn;
1317 if (!get_http_auth(smp, htx))
1318 return 0;
1319
1320 smp->data.type = SMP_T_STR;
1321 smp->data.u.str.area = txn->auth.user;
1322 smp->data.u.str.data = strlen(txn->auth.user);
1323 smp->flags = SMP_F_CONST;
1324 return 1;
1325}
1326
1327/* Fetch the password supplied if there is an Authorization header. It relies on
1328 * get_http_auth()
1329 */
1330static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1331{
1332 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001333 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001334 struct http_txn *txn;
1335
1336 if (!htx)
1337 return 0;
1338
1339 txn = smp->strm->txn;
1340 if (!get_http_auth(smp, htx))
1341 return 0;
1342
1343 smp->data.type = SMP_T_STR;
1344 smp->data.u.str.area = txn->auth.pass;
1345 smp->data.u.str.data = strlen(txn->auth.pass);
1346 smp->flags = SMP_F_CONST;
1347 return 1;
1348}
1349
Willy Tarreau79e57332018-10-02 16:01:16 +02001350/* Accepts exactly 1 argument of type userlist */
1351static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1352{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001353 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001354 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001355
Christopher Faulet623af932021-01-29 11:22:15 +01001356 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001357 return 0;
1358
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001359 if (!htx)
1360 return 0;
1361 if (!get_http_auth(smp, htx))
1362 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001363
1364 smp->data.type = SMP_T_BOOL;
1365 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001366 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001367 return 1;
1368}
1369
1370/* Accepts exactly 1 argument of type userlist */
1371static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1372{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001373 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001374 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001375
Christopher Faulet623af932021-01-29 11:22:15 +01001376 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001377 return 0;
1378
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001379 if (!htx)
1380 return 0;
1381 if (!get_http_auth(smp, htx))
1382 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001383
Willy Tarreau79e57332018-10-02 16:01:16 +02001384 /* if the user does not belong to the userlist or has a wrong password,
1385 * report that it unconditionally does not match. Otherwise we return
1386 * a string containing the username.
1387 */
1388 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1389 smp->strm->txn->auth.pass))
1390 return 0;
1391
1392 /* pat_match_auth() will need the user list */
1393 smp->ctx.a[0] = args->data.usr;
1394
1395 smp->data.type = SMP_T_STR;
1396 smp->flags = SMP_F_CONST;
1397 smp->data.u.str.area = smp->strm->txn->auth.user;
1398 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1399
1400 return 1;
1401}
1402
1403/* Fetch a captured HTTP request header. The index is the position of
1404 * the "capture" option in the configuration file
1405 */
1406static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1407{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001408 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001409 int idx;
1410
Christopher Faulet623af932021-01-29 11:22:15 +01001411 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001412 return 0;
1413
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001414 if (!smp->strm)
1415 return 0;
1416
1417 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001418 idx = args->data.sint;
1419
1420 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1421 return 0;
1422
1423 smp->data.type = SMP_T_STR;
1424 smp->flags |= SMP_F_CONST;
1425 smp->data.u.str.area = smp->strm->req_cap[idx];
1426 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1427
1428 return 1;
1429}
1430
1431/* Fetch a captured HTTP response header. The index is the position of
1432 * the "capture" option in the configuration file
1433 */
1434static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1435{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001436 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001437 int idx;
1438
Christopher Faulet623af932021-01-29 11:22:15 +01001439 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001440 return 0;
1441
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001442 if (!smp->strm)
1443 return 0;
1444
1445 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001446 idx = args->data.sint;
1447
1448 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1449 return 0;
1450
1451 smp->data.type = SMP_T_STR;
1452 smp->flags |= SMP_F_CONST;
1453 smp->data.u.str.area = smp->strm->res_cap[idx];
1454 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1455
1456 return 1;
1457}
1458
1459/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1460static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1461{
1462 struct buffer *temp;
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001463 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001464 char *ptr;
1465
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001466 if (!smp->strm)
1467 return 0;
1468
1469 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001470 if (!txn || !txn->uri)
1471 return 0;
1472
1473 ptr = txn->uri;
1474
1475 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1476 ptr++;
1477
1478 temp = get_trash_chunk();
1479 temp->area = txn->uri;
1480 temp->data = ptr - txn->uri;
1481 smp->data.u.str = *temp;
1482 smp->data.type = SMP_T_STR;
1483 smp->flags = SMP_F_CONST;
1484
1485 return 1;
1486
1487}
1488
1489/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1490static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1491{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001492 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001493 struct ist path;
1494 const char *ptr;
1495
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001496 if (!smp->strm)
1497 return 0;
1498
1499 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001500 if (!txn || !txn->uri)
1501 return 0;
1502
1503 ptr = txn->uri;
1504
1505 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1506 ptr++;
1507
1508 if (!*ptr)
1509 return 0;
1510
Christopher Faulet78337bb2018-11-15 14:35:18 +01001511 /* skip the first space and find space after URI */
1512 path = ist2(++ptr, 0);
1513 while (*ptr != ' ' && *ptr != '\0')
1514 ptr++;
1515 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001516
Christopher Faulet78337bb2018-11-15 14:35:18 +01001517 path = http_get_path(path);
Tim Duesterhused526372020-03-05 17:56:33 +01001518 if (!isttest(path))
Willy Tarreau79e57332018-10-02 16:01:16 +02001519 return 0;
1520
1521 smp->data.u.str.area = path.ptr;
1522 smp->data.u.str.data = path.len;
1523 smp->data.type = SMP_T_STR;
1524 smp->flags = SMP_F_CONST;
1525
1526 return 1;
1527}
1528
1529/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1530 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1531 */
1532static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1533{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001534 struct http_txn *txn;
1535
1536 if (!smp->strm)
1537 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001538
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001539 txn = smp->strm->txn;
Christopher Faulet09f88362021-04-01 16:00:29 +02001540 if (!txn || txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001541 return 0;
1542
1543 if (txn->req.flags & HTTP_MSGF_VER_11)
1544 smp->data.u.str.area = "HTTP/1.1";
1545 else
1546 smp->data.u.str.area = "HTTP/1.0";
1547
1548 smp->data.u.str.data = 8;
1549 smp->data.type = SMP_T_STR;
1550 smp->flags = SMP_F_CONST;
1551 return 1;
1552
1553}
1554
1555/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1556 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1557 */
1558static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1559{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001560 struct http_txn *txn;
1561
1562 if (!smp->strm)
1563 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001564
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001565 txn = smp->strm->txn;
Christopher Faulet09f88362021-04-01 16:00:29 +02001566 if (!txn || txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001567 return 0;
1568
1569 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1570 smp->data.u.str.area = "HTTP/1.1";
1571 else
1572 smp->data.u.str.area = "HTTP/1.0";
1573
1574 smp->data.u.str.data = 8;
1575 smp->data.type = SMP_T_STR;
1576 smp->flags = SMP_F_CONST;
1577 return 1;
1578
1579}
1580
1581/* Iterate over all cookies present in a message. The context is stored in
1582 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1583 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1584 * the direction, multiple cookies may be parsed on the same line or not.
Maciej Zdebdea7c202020-11-13 09:38:06 +00001585 * If provided, the searched cookie name is in args, in args->data.str. If
1586 * the input options indicate that no iterating is desired, then only last
1587 * value is fetched if any. If no cookie name is provided, the first cookie
1588 * value found is fetched. The returned sample is of type CSTR. Can be used
1589 * to parse cookies in other files.
Willy Tarreau79e57332018-10-02 16:01:16 +02001590 */
1591static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1592{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001593 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1594 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001595 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001596 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001597 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1598 struct ist hdr;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001599 char *cook = NULL;
1600 size_t cook_l = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001601 int found = 0;
1602
Christopher Faulet623af932021-01-29 11:22:15 +01001603 if (args->type == ARGT_STR) {
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001604 cook = args->data.str.area;
1605 cook_l = args->data.str.data;
1606 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001607
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001608 if (!ctx) {
1609 /* first call */
1610 ctx = &static_http_hdr_ctx;
1611 ctx->blk = NULL;
1612 smp->ctx.a[2] = ctx;
1613 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001614
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001615 if (!htx)
1616 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001617
Christopher Faulet16032ab2020-04-30 11:30:00 +02001618 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001619
Maciej Zdebdea7c202020-11-13 09:38:06 +00001620 /* OK so basically here, either we want only one value or we want to
1621 * iterate over all of them and we fetch the next one. In this last case
1622 * SMP_OPT_ITERATE option is set.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001623 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001624
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001625 if (!(smp->flags & SMP_F_NOT_LAST)) {
1626 /* search for the header from the beginning, we must first initialize
1627 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001628 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001629 smp->ctx.a[0] = NULL;
1630 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001631 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001632
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001633 smp->flags |= SMP_F_VOL_HDR;
1634 while (1) {
1635 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1636 if (!smp->ctx.a[0]) {
1637 if (!http_find_header(htx, hdr, ctx, 0))
1638 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001639
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001640 if (ctx->value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001641 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001642
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001643 smp->ctx.a[0] = ctx->value.ptr;
1644 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001645 }
1646
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001647 smp->data.type = SMP_T_STR;
1648 smp->flags |= SMP_F_CONST;
1649 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001650 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001651 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1652 &smp->data.u.str.area,
1653 &smp->data.u.str.data);
1654 if (smp->ctx.a[0]) {
1655 found = 1;
Maciej Zdebdea7c202020-11-13 09:38:06 +00001656 if (smp->opt & SMP_OPT_ITERATE) {
1657 /* iterate on cookie value */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001658 smp->flags |= SMP_F_NOT_LAST;
1659 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001660 }
Maciej Zdebdea7c202020-11-13 09:38:06 +00001661 if (args->data.str.data == 0) {
1662 /* No cookie name, first occurrence returned */
1663 break;
1664 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001665 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001666 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001667 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001668
Willy Tarreau79e57332018-10-02 16:01:16 +02001669 /* all cookie headers and values were scanned. If we're looking for the
1670 * last occurrence, we may return it now.
1671 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001672 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001673 smp->flags &= ~SMP_F_NOT_LAST;
1674 return found;
1675}
1676
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001677/* Same than smp_fetch_cookie() but only relies on the sample direction to
1678 * choose the right channel. So instead of duplicating the code, we just change
1679 * the keyword and then fallback on smp_fetch_cookie().
1680 */
1681static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1682{
1683 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1684 return smp_fetch_cookie(args, smp, kw, private);
1685}
1686
Willy Tarreau79e57332018-10-02 16:01:16 +02001687/* Iterate over all cookies present in a request to count how many occurrences
1688 * match the name in args and args->data.str.len. If <multi> is non-null, then
1689 * multiple cookies may be parsed on the same line. The returned sample is of
1690 * type UINT. Accepts exactly 1 argument of type string.
1691 */
1692static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1693{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001694 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1695 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001696 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001697 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001698 struct http_hdr_ctx ctx;
1699 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001700 char *val_beg, *val_end;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001701 char *cook = NULL;
1702 size_t cook_l = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001703 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001704
Christopher Faulet623af932021-01-29 11:22:15 +01001705 if (args->type == ARGT_STR){
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001706 cook = args->data.str.area;
1707 cook_l = args->data.str.data;
1708 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001709
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001710 if (!htx)
1711 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001712
Christopher Faulet16032ab2020-04-30 11:30:00 +02001713 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001714
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001715 val_end = val_beg = NULL;
1716 ctx.blk = NULL;
1717 cnt = 0;
1718 while (1) {
1719 /* Note: val_beg == NULL every time we need to fetch a new header */
1720 if (!val_beg) {
1721 if (!http_find_header(htx, hdr, &ctx, 0))
1722 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001723
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001724 if (ctx.value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001725 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001726
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001727 val_beg = ctx.value.ptr;
1728 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001729 }
1730
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001731 smp->data.type = SMP_T_STR;
1732 smp->flags |= SMP_F_CONST;
1733 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001734 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001735 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1736 &smp->data.u.str.area,
1737 &smp->data.u.str.data))) {
1738 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001739 }
1740 }
1741
1742 smp->data.type = SMP_T_SINT;
1743 smp->data.u.sint = cnt;
1744 smp->flags |= SMP_F_VOL_HDR;
1745 return 1;
1746}
1747
1748/* Fetch an cookie's integer value. The integer value is returned. It
1749 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1750 */
1751static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1752{
1753 int ret = smp_fetch_cookie(args, smp, kw, private);
1754
1755 if (ret > 0) {
1756 smp->data.type = SMP_T_SINT;
1757 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1758 smp->data.u.str.data);
1759 }
1760
1761 return ret;
1762}
1763
1764/************************************************************************/
1765/* The code below is dedicated to sample fetches */
1766/************************************************************************/
1767
1768/* This scans a URL-encoded query string. It takes an optionally wrapping
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001769 * string whose first contiguous chunk has its beginning in ctx->a[0] and end
Willy Tarreau79e57332018-10-02 16:01:16 +02001770 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1771 * pointers are updated for next iteration before leaving.
1772 */
1773static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1774{
1775 const char *vstart, *vend;
1776 struct buffer *temp;
1777 const char **chunks = (const char **)smp->ctx.a;
1778
1779 if (!http_find_next_url_param(chunks, name, name_len,
1780 &vstart, &vend, delim))
1781 return 0;
1782
1783 /* Create sample. If the value is contiguous, return the pointer as CONST,
1784 * if the value is wrapped, copy-it in a buffer.
1785 */
1786 smp->data.type = SMP_T_STR;
1787 if (chunks[2] &&
1788 vstart >= chunks[0] && vstart <= chunks[1] &&
1789 vend >= chunks[2] && vend <= chunks[3]) {
1790 /* Wrapped case. */
1791 temp = get_trash_chunk();
1792 memcpy(temp->area, vstart, chunks[1] - vstart);
1793 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1794 vend - chunks[2]);
1795 smp->data.u.str.area = temp->area;
1796 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1797 } else {
1798 /* Contiguous case. */
1799 smp->data.u.str.area = (char *)vstart;
1800 smp->data.u.str.data = vend - vstart;
1801 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1802 }
1803
1804 /* Update context, check wrapping. */
1805 chunks[0] = vend;
1806 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1807 chunks[1] = chunks[3];
1808 chunks[2] = NULL;
1809 }
1810
1811 if (chunks[0] < chunks[1])
1812 smp->flags |= SMP_F_NOT_LAST;
1813
1814 return 1;
1815}
1816
1817/* This function iterates over each parameter of the query string. It uses
1818 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1819 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1820 * An optional parameter name is passed in args[0], otherwise any parameter is
1821 * considered. It supports an optional delimiter argument for the beginning of
1822 * the string in args[1], which defaults to "?".
1823 */
1824static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1825{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001826 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001827 char delim = '?';
1828 const char *name;
1829 int name_len;
1830
Christopher Faulet623af932021-01-29 11:22:15 +01001831 if ((args[0].type && args[0].type != ARGT_STR) ||
Willy Tarreau79e57332018-10-02 16:01:16 +02001832 (args[1].type && args[1].type != ARGT_STR))
1833 return 0;
1834
1835 name = "";
1836 name_len = 0;
1837 if (args->type == ARGT_STR) {
1838 name = args->data.str.area;
1839 name_len = args->data.str.data;
1840 }
1841
1842 if (args[1].type)
1843 delim = *args[1].data.str.area;
1844
1845 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001846 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001847 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001848
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001849 if (!htx)
1850 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001851
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001852 sl = http_get_stline(htx);
1853 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1854 if (!smp->ctx.a[0])
1855 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001856
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001857 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001858
1859 /* Assume that the context is filled with NULL pointer
1860 * before the first call.
1861 * smp->ctx.a[2] = NULL;
1862 * smp->ctx.a[3] = NULL;
1863 */
1864 }
1865
1866 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1867}
1868
1869/* This function iterates over each parameter of the body. This requires
1870 * that the body has been waited for using http-buffer-request. It uses
1871 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001872 * contiguous part of the body, and optionally ctx->a[2..3] to reference the
Willy Tarreau79e57332018-10-02 16:01:16 +02001873 * optional second part if the body wraps at the end of the buffer. An optional
1874 * parameter name is passed in args[0], otherwise any parameter is considered.
1875 */
1876static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1877{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001878 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001879 const char *name;
1880 int name_len;
1881
Christopher Faulet623af932021-01-29 11:22:15 +01001882 if (args[0].type && args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001883 return 0;
1884
1885 name = "";
1886 name_len = 0;
1887 if (args[0].type == ARGT_STR) {
1888 name = args[0].data.str.area;
1889 name_len = args[0].data.str.data;
1890 }
1891
1892 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulete596d182020-05-05 17:46:34 +02001893 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001894 struct buffer *temp;
1895 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001896
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001897 if (!htx)
1898 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001899
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001900 temp = get_trash_chunk();
1901 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1902 struct htx_blk *blk = htx_get_blk(htx, pos);
1903 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001904
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001905 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001906 break;
1907 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001908 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001909 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001910 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001911 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001912
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001913 smp->ctx.a[0] = temp->area;
1914 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001915
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001916 /* Assume that the context is filled with NULL pointer
1917 * before the first call.
1918 * smp->ctx.a[2] = NULL;
1919 * smp->ctx.a[3] = NULL;
1920 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001921
Willy Tarreau79e57332018-10-02 16:01:16 +02001922 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001923
Willy Tarreau79e57332018-10-02 16:01:16 +02001924 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1925}
1926
1927/* Return the signed integer value for the specified url parameter (see url_param
1928 * above).
1929 */
1930static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1931{
1932 int ret = smp_fetch_url_param(args, smp, kw, private);
1933
1934 if (ret > 0) {
1935 smp->data.type = SMP_T_SINT;
1936 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1937 smp->data.u.str.data);
1938 }
1939
1940 return ret;
1941}
1942
1943/* This produces a 32-bit hash of the concatenation of the first occurrence of
1944 * the Host header followed by the path component if it begins with a slash ('/').
1945 * This means that '*' will not be added, resulting in exactly the first Host
1946 * entry. If no Host header is found, then the path is used. The resulting value
1947 * is hashed using the url hash followed by a full avalanche hash and provides a
1948 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1949 * high-traffic sites without having to store whole paths.
1950 * this differs from the base32 functions in that it includes the url parameters
1951 * as well as the path
1952 */
1953static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1954{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001955 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001956 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001957 struct http_hdr_ctx ctx;
1958 struct htx_sl *sl;
1959 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001960 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001961
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001962 if (!htx)
1963 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001964
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001965 ctx.blk = NULL;
1966 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1967 /* OK we have the header value in ctx.value */
1968 while (ctx.value.len--)
1969 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001970 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001971
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001972 /* now retrieve the path */
1973 sl = http_get_stline(htx);
1974 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001975 if (path.len && *(path.ptr) == '/') {
1976 while (path.len--)
1977 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001978 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001979
Willy Tarreau79e57332018-10-02 16:01:16 +02001980 hash = full_hash(hash);
1981
1982 smp->data.type = SMP_T_SINT;
1983 smp->data.u.sint = hash;
1984 smp->flags = SMP_F_VOL_1ST;
1985 return 1;
1986}
1987
1988/* This concatenates the source address with the 32-bit hash of the Host and
1989 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1990 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1991 * on the source address length. The URL hash is stored before the address so
1992 * that in environments where IPv6 is insignificant, truncating the output to
1993 * 8 bytes would still work.
1994 */
1995static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1996{
1997 struct buffer *temp;
1998 struct connection *cli_conn = objt_conn(smp->sess->origin);
1999
Willy Tarreaucd7ca792019-07-17 16:57:03 +02002000 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02002001 return 0;
2002
2003 if (!smp_fetch_url32(args, smp, kw, private))
2004 return 0;
2005
2006 temp = get_trash_chunk();
2007 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2008 temp->data += sizeof(unsigned int);
2009
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002010 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02002011 case AF_INET:
2012 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002013 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002014 4);
2015 temp->data += 4;
2016 break;
2017 case AF_INET6:
2018 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002019 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002020 16);
2021 temp->data += 16;
2022 break;
2023 default:
2024 return 0;
2025 }
2026
2027 smp->data.u.str = *temp;
2028 smp->data.type = SMP_T_BIN;
2029 return 1;
2030}
2031
2032/************************************************************************/
2033/* Other utility functions */
2034/************************************************************************/
2035
2036/* This function is used to validate the arguments passed to any "hdr" fetch
2037 * keyword. These keywords support an optional positive or negative occurrence
2038 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2039 * is assumed that the types are already the correct ones. Returns 0 on error,
2040 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2041 * error message in case of error, that the caller is responsible for freeing.
2042 * The initial location must either be freeable or NULL.
2043 * Note: this function's pointer is checked from Lua.
2044 */
2045int val_hdr(struct arg *arg, char **err_msg)
2046{
2047 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2048 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2049 return 0;
2050 }
2051 return 1;
2052}
2053
2054/************************************************************************/
2055/* All supported sample fetch keywords must be declared here. */
2056/************************************************************************/
2057
2058/* Note: must not be declared <const> as its list will be overwritten */
2059static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2060 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2061 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2062 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Yves Lafonb4d37082021-02-11 11:01:28 +01002063 { "baseq", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002064
2065 /* capture are allocated and are permanent in the stream */
2066 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2067
2068 /* retrieve these captures from the HTTP logs */
2069 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2070 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2071 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2072
2073 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2074 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2075
2076 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2077 * are only here to match the ACL's name, are request-only and are used
2078 * for ACL compatibility only.
2079 */
2080 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002081 { "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 +02002082 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2083 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2084
2085 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2086 * only here to match the ACL's name, are request-only and are used for
2087 * ACL compatibility only.
2088 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002089 { "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 +02002090 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2091 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2092 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2093
Christopher Fauleta4063562019-08-02 11:51:37 +02002094 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2095 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2096 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002097 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2098 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2099 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2100 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2101 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Faulete720c322020-09-02 17:25:18 +02002102 { "pathq", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002103 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2104
2105 /* HTTP protocol on the request path */
2106 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2107 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2108
2109 /* HTTP version on the request path */
2110 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2111 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2112
2113 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2114 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2115 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2116 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2117
2118 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2119 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2120
2121 /* HTTP version on the response path */
2122 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2123 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2124
Christopher Faulete596d182020-05-05 17:46:34 +02002125 { "res.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2126 { "res.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2127 { "res.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2128
2129 { "res.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2130 { "res.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2131
Willy Tarreau79e57332018-10-02 16:01:16 +02002132 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2133 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2134 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2135 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2136
2137 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2138 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2139 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2140 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2141 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2142 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2143 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2144
2145 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2146 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2147 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2148 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2149
2150 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2151 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2152 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2153 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2154 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2155 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2156 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2157
2158 /* scook is valid only on the response and is used for ACL compatibility */
2159 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2160 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2161 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2162 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2163
2164 /* shdr is valid only on the response and is used for ACL compatibility */
2165 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2166 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2167 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2168 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2169
2170 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2171 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2172 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2173 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2174 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2175 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2176 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2177 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2178 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2179 { "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 +02002180
Willy Tarreau79e57332018-10-02 16:01:16 +02002181 { /* END */ },
2182}};
2183
Willy Tarreau0108d902018-11-25 19:14:37 +01002184INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002185
2186/*
2187 * Local variables:
2188 * c-indent-level: 8
2189 * c-basic-offset: 8
2190 * End:
2191 */