blob: d3192aa6c47a3a4b165a5588715ca84803cb97c7 [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
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200123 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200124
125 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
126 struct buffer *http_auth = get_trash_chunk();
127
128 len = base64dec(txn->auth.method_data.area,
129 txn->auth.method_data.data,
130 http_auth->area, global.tune.bufsize - 1);
131
132 if (len < 0)
133 return 0;
134
135
136 http_auth->area[len] = '\0';
137
138 p = strchr(http_auth->area, ':');
139
140 if (!p)
141 return 0;
142
143 txn->auth.user = http_auth->area;
144 *p = '\0';
145 txn->auth.pass = p+1;
146
147 txn->auth.method = HTTP_AUTH_BASIC;
148 return 1;
149 }
150
151 return 0;
152}
153
154/* This function ensures that the prerequisites for an L7 fetch are ready,
155 * which means that a request or response is ready. If some data is missing,
156 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200157 * to extract data from L7. If <vol> is non-null during a prefetch, another
158 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200159 *
160 * The function returns :
161 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
162 * decide whether or not an HTTP message is present ;
163 * NULL if the requested data cannot be fetched or if it is certain that
Willy Tarreaueae83722020-04-29 11:52:51 +0200164 * we'll never have any HTTP message there; this includes null strm or chn.
Willy Tarreaua6d98792020-08-12 14:04:52 +0200165 * NULL if the sample's direction does not match the channel's (i.e. the
166 * function was asked to work on the wrong channel)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200167 * The HTX message if ready
168 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200169struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, struct check *check, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200170{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200171 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200172 struct http_txn *txn = NULL;
173 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200174 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100175 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200176
Willy Tarreaua6d98792020-08-12 14:04:52 +0200177 if (chn &&
178 (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ && (chn->flags & CF_ISRESP)) ||
179 ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES && !(chn->flags & CF_ISRESP))))
180 return 0;
181
Christopher Fauletef453ed2018-10-24 21:39:27 +0200182 /* Note: it is possible that <s> is NULL when called before stream
183 * initialization (eg: tcp-request connection), so this function is the
184 * one responsible for guarding against this case for all HTTP users.
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200185 *
186 * In the health check context, the stream and the channel must be NULL
187 * and <check> must be set. In this case, only the input buffer,
188 * corresponding to the response, is considered. It is the caller
189 * responsibility to provide <check>.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200190 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200191 BUG_ON(check && (s || chn));
192 if (!s || !chn) {
193 if (check) {
194 htx = htxbuf(&check->bi);
195
196 /* Analyse not yet started */
197 if (htx_is_empty(htx) || htx->first == -1)
198 return NULL;
199
200 sl = http_get_stline(htx);
201 if (vol && !sl) {
202 /* The start-line was already forwarded, it is too late to fetch anything */
203 return NULL;
204 }
205 goto end;
206 }
207
Christopher Fauletef453ed2018-10-24 21:39:27 +0200208 return NULL;
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200209 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200210
Christopher Faulet75f619a2021-03-08 19:12:58 +0100211 if (!s->txn && !http_create_txn(s))
212 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200213 txn = s->txn;
214 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200215
Christopher Fauleteca88542019-04-03 10:12:42 +0200216 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200217 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200218
Christopher Faulet89dc4992019-04-17 12:02:59 +0200219 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
220 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200221
Christopher Faulet89dc4992019-04-17 12:02:59 +0200222 if (msg->msg_state < HTTP_MSG_BODY) {
223 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200224 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200225 /* Parsing is done by the mux, just wait */
226 smp->flags |= SMP_F_MAY_CHANGE;
227 return NULL;
228 }
229 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200230 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200231 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200232 /* The start-line was already forwarded, it is too late to fetch anything */
233 return NULL;
234 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200235 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200236 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200237 struct buffer *buf;
238 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200239 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200240 union h1_sl h1sl;
241 unsigned int flags = HTX_FL_NONE;
242 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200243
Christopher Faulet89dc4992019-04-17 12:02:59 +0200244 /* no HTTP fetch on the response in TCP mode */
245 if (chn->flags & CF_ISRESP)
246 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200247
Christopher Faulet89dc4992019-04-17 12:02:59 +0200248 /* Now we are working on the request only */
249 buf = &chn->buf;
250 if (b_head(buf) + b_data(buf) > b_wrap(buf))
251 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200252
Christopher Faulet89dc4992019-04-17 12:02:59 +0200253 h1m_init_req(&h1m);
254 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
255 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
256 if (ret <= 0) {
257 /* Invalid or too big*/
258 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200259 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100260
Christopher Faulet89dc4992019-04-17 12:02:59 +0200261 /* wait for a full request */
262 smp->flags |= SMP_F_MAY_CHANGE;
263 return NULL;
264 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100265
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500266 /* OK we just got a valid HTTP message. We have to convert it
Christopher Faulet89dc4992019-04-17 12:02:59 +0200267 * into an HTX message.
268 */
269 if (unlikely(h1sl.rq.v.len == 0)) {
270 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
271 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200272 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200273 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200274 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200275
276 /* Set HTX start-line flags */
277 if (h1m.flags & H1_MF_VER_11)
278 flags |= HTX_SL_F_VER_11;
279 if (h1m.flags & H1_MF_XFER_ENC)
280 flags |= HTX_SL_F_XFER_ENC;
281 flags |= HTX_SL_F_XFER_LEN;
282 if (h1m.flags & H1_MF_CHNK)
283 flags |= HTX_SL_F_CHNK;
284 else if (h1m.flags & H1_MF_CLEN)
285 flags |= HTX_SL_F_CLEN;
286
Richard Russo458eafb2019-07-31 11:45:56 -0700287 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200288 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
289 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200290 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200291 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200292 }
293
294 /* OK we just got a valid HTTP message. If not already done by
295 * HTTP analyzers, we have some minor preparation to perform so
296 * that further checks can rely on HTTP tests.
297 */
298 if (sl && msg->msg_state < HTTP_MSG_BODY) {
299 if (!(chn->flags & CF_ISRESP)) {
300 txn->meth = sl->info.req.meth;
301 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
302 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200303 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200304 else
305 txn->status = sl->info.res.status;
306 if (sl->flags & HTX_SL_F_VER_11)
307 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200308 }
309
310 /* everything's OK */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200311 end:
Christopher Fauletef453ed2018-10-24 21:39:27 +0200312 return htx;
313}
314
Willy Tarreau79e57332018-10-02 16:01:16 +0200315/* This function fetches the method of current HTTP request and stores
316 * it in the global pattern struct as a chunk. There are two possibilities :
317 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
318 * in <len> and <ptr> is NULL ;
319 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
320 * <len> to its length.
321 * This is intended to be used with pat_match_meth() only.
322 */
323static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
324{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200325 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200326 struct http_txn *txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200327 int meth;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200328
Willy Tarreaua6d98792020-08-12 14:04:52 +0200329 txn = smp->strm->txn;
330 if (!txn)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200331 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200332
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200333 meth = txn->meth;
334 smp->data.type = SMP_T_METH;
335 smp->data.u.meth.meth = meth;
336 if (meth == HTTP_METH_OTHER) {
Willy Tarreaua6d98792020-08-12 14:04:52 +0200337 struct htx *htx;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200338 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200339
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200340 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
341 /* ensure the indexes are not affected */
342 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200343 }
Willy Tarreaua6d98792020-08-12 14:04:52 +0200344
Christopher Faulet6f97a612021-04-15 09:28:02 +0200345 htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreaua6d98792020-08-12 14:04:52 +0200346 if (!htx)
347 return 0;
348
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200349 sl = http_get_stline(htx);
350 smp->flags |= SMP_F_CONST;
351 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
352 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200353 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200354 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200355 return 1;
356}
357
358static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
359{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200360 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200361 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200362 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200363 char *ptr;
364 int len;
365
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200366 if (!htx)
367 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200368
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200369 sl = http_get_stline(htx);
370 len = HTX_SL_REQ_VLEN(sl);
371 ptr = HTX_SL_REQ_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200372
373 while ((len-- > 0) && (*ptr++ != '/'));
374 if (len <= 0)
375 return 0;
376
377 smp->data.type = SMP_T_STR;
378 smp->data.u.str.area = ptr;
379 smp->data.u.str.data = len;
380
381 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
382 return 1;
383}
384
385static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
386{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200387 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200388 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200389 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200390 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200391 char *ptr;
392 int len;
393
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200394 if (!htx)
395 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200396
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200397 sl = http_get_stline(htx);
398 len = HTX_SL_RES_VLEN(sl);
399 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200400
401 while ((len-- > 0) && (*ptr++ != '/'));
402 if (len <= 0)
403 return 0;
404
405 smp->data.type = SMP_T_STR;
406 smp->data.u.str.area = ptr;
407 smp->data.u.str.data = len;
408
409 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
410 return 1;
411}
412
413/* 3. Check on Status Code. We manipulate integers here. */
414static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
415{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200416 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200417 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200418 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200419 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200420 char *ptr;
421 int len;
422
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200423 if (!htx)
424 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200425
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200426 sl = http_get_stline(htx);
427 len = HTX_SL_RES_CLEN(sl);
428 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200429
430 smp->data.type = SMP_T_SINT;
431 smp->data.u.sint = __strl2ui(ptr, len);
432 smp->flags = SMP_F_VOL_1ST;
433 return 1;
434}
435
436static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
437{
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100438 struct ist unique_id;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100439
Willy Tarreau79e57332018-10-02 16:01:16 +0200440 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
441 return 0;
442
Willy Tarreaua1062a42020-04-29 11:50:38 +0200443 if (!smp->strm)
444 return 0;
445
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100446 unique_id = stream_generate_unique_id(smp->strm, &smp->sess->fe->format_unique_id);
447 if (!isttest(unique_id))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100448 return 0;
449
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100450 smp->data.u.str.area = smp->strm->unique_id.ptr;
451 smp->data.u.str.data = smp->strm->unique_id.len;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100452 smp->data.type = SMP_T_STR;
Willy Tarreau79e57332018-10-02 16:01:16 +0200453 smp->flags = SMP_F_CONST;
454 return 1;
455}
456
457/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800458 * empty line which separes headers from the body. This is useful
459 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200460 */
461static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
462{
Christopher Faulete596d182020-05-05 17:46:34 +0200463 /* possible keywords: req.hdrs, res.hdrs */
464 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200465 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200466 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200467 struct buffer *temp;
468 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200469
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200470 if (!htx)
471 return 0;
472 temp = get_trash_chunk();
473 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
474 struct htx_blk *blk = htx_get_blk(htx, pos);
475 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200476
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200477 if (type == HTX_BLK_HDR) {
478 struct ist n = htx_get_blk_name(htx, blk);
479 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200480
Christopher Faulet53a899b2019-10-08 16:38:42 +0200481 if (!h1_format_htx_hdr(n, v, temp))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200482 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200483 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200484 else if (type == HTX_BLK_EOH) {
485 if (!chunk_memcat(temp, "\r\n", 2))
486 return 0;
487 break;
488 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200489 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200490 smp->data.type = SMP_T_STR;
491 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200492 return 1;
493}
494
495/* Returns the header request in a length/value encoded format.
496 * This is useful for exchanges with the SPOE.
497 *
498 * A "length value" is a multibyte code encoding numbers. It uses the
499 * SPOE format. The encoding is the following:
500 *
501 * Each couple "header name" / "header value" is composed
502 * like this:
503 * "length value" "header name bytes"
504 * "length value" "header value bytes"
505 * When the last header is reached, the header name and the header
506 * value are empty. Their length are 0
507 */
508static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
509{
Christopher Faulete596d182020-05-05 17:46:34 +0200510 /* possible keywords: req.hdrs_bin, res.hdrs_bin */
511 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200512 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200513 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200514 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200515 char *p, *end;
516 int32_t pos;
517 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200518
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200519 if (!htx)
520 return 0;
521 temp = get_trash_chunk();
522 p = temp->area;
523 end = temp->area + temp->size;
524 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
525 struct htx_blk *blk = htx_get_blk(htx, pos);
526 enum htx_blk_type type = htx_get_blk_type(blk);
527 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200528
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200529 if (type == HTX_BLK_HDR) {
530 n = htx_get_blk_name(htx,blk);
531 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200532
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200533 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200534 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200535 if (ret == -1)
536 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200537 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200538 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200539 memcpy(p, n.ptr, n.len);
540 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200541
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200542 /* encode the header value. */
543 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200544 if (ret == -1)
545 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200546 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200547 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200548 memcpy(p, v.ptr, v.len);
549 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200550
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200551 }
552 else if (type == HTX_BLK_EOH) {
553 /* encode the end of the header list with empty
554 * header name and header value.
555 */
556 ret = encode_varint(0, &p, end);
557 if (ret == -1)
558 return 0;
559 ret = encode_varint(0, &p, end);
560 if (ret == -1)
561 return 0;
562 break;
563 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200564 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200565
566 /* Initialise sample data which will be filled. */
567 smp->data.type = SMP_T_BIN;
568 smp->data.u.str.area = temp->area;
569 smp->data.u.str.data = p - temp->area;
570 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200571 return 1;
572}
573
574/* returns the longest available part of the body. This requires that the body
575 * has been waited for using http-buffer-request.
576 */
577static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
578{
Christopher Faulete596d182020-05-05 17:46:34 +0200579 /* possible keywords: req.body, res.body */
580 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200581 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200582 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200583 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200584 int32_t pos;
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100585 int finished = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200586
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200587 if (!htx)
588 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200589
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200590 temp = get_trash_chunk();
591 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
592 struct htx_blk *blk = htx_get_blk(htx, pos);
593 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200594
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100595 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) {
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100596 finished = 1;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200597 break;
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100598 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200599 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +0200600 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200601 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200602 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200603 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200604
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200605 smp->data.type = SMP_T_BIN;
606 smp->data.u.str = *temp;
607 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200608
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100609 if (!finished && (check || (chn && !channel_full(chn, global.tune.maxrewrite) &&
610 !(chn->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)))))
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200611 smp->flags |= SMP_F_MAY_CHANGE;
612
Willy Tarreau79e57332018-10-02 16:01:16 +0200613 return 1;
614}
615
616
617/* returns the available length of the body. This requires that the body
618 * has been waited for using http-buffer-request.
619 */
620static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
621{
Christopher Faulete596d182020-05-05 17:46:34 +0200622 /* possible keywords: req.body_len, res.body_len */
623 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200624 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200625 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200626 int32_t pos;
627 unsigned long long len = 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100628
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200629 if (!htx)
630 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100631
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200632 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
633 struct htx_blk *blk = htx_get_blk(htx, pos);
634 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100635
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100636 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200637 break;
638 if (type == HTX_BLK_DATA)
639 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200640 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200641
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200642 smp->data.type = SMP_T_SINT;
643 smp->data.u.sint = len;
644 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200645 return 1;
646}
647
648
649/* returns the advertised length of the body, or the advertised size of the
650 * chunks available in the buffer. This requires that the body has been waited
651 * for using http-buffer-request.
652 */
653static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
654{
Christopher Faulete596d182020-05-05 17:46:34 +0200655 /* possible keywords: req.body_size, res.body_size */
656 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200657 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200658 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200659 int32_t pos;
660 unsigned long long len = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200661
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200662 if (!htx)
663 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100664
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200665 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
666 struct htx_blk *blk = htx_get_blk(htx, pos);
667 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100668
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100669 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200670 break;
671 if (type == HTX_BLK_DATA)
672 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200673 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200674 if (htx->extra != ULLONG_MAX)
675 len += htx->extra;
Willy Tarreau79e57332018-10-02 16:01:16 +0200676
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200677 smp->data.type = SMP_T_SINT;
678 smp->data.u.sint = len;
679 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200680 return 1;
681}
682
683
684/* 4. Check on URL/URI. A pointer to the URI is stored. */
685static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
686{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200687 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200688 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200689 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200690
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200691 if (!htx)
692 return 0;
693 sl = http_get_stline(htx);
694 smp->data.type = SMP_T_STR;
695 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
696 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
697 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200698 return 1;
699}
700
701static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
702{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200703 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200704 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200705 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200706 struct sockaddr_storage addr;
707
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200708 memset(&addr, 0, sizeof(addr));
709
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200710 if (!htx)
711 return 0;
712 sl = http_get_stline(htx);
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200713 if (url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL) < 0)
714 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200715
Willy Tarreau48584642021-05-09 10:32:54 +0200716 if (addr.ss_family != AF_INET)
Willy Tarreau79e57332018-10-02 16:01:16 +0200717 return 0;
718
719 smp->data.type = SMP_T_IPV4;
720 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
721 smp->flags = 0;
722 return 1;
723}
724
725static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
726{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200727 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200728 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200729 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200730 struct sockaddr_storage addr;
731
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200732 memset(&addr, 0, sizeof(addr));
733
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200734 if (!htx)
735 return 0;
736 sl = http_get_stline(htx);
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200737 if (url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL) < 0)
738 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200739
Willy Tarreau48584642021-05-09 10:32:54 +0200740 if (addr.ss_family != AF_INET)
Willy Tarreau79e57332018-10-02 16:01:16 +0200741 return 0;
742
743 smp->data.type = SMP_T_SINT;
Willy Tarreau48584642021-05-09 10:32:54 +0200744 smp->data.u.sint = get_host_port(&addr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200745 smp->flags = 0;
746 return 1;
747}
748
749/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
750 * Accepts an optional argument of type string containing the header field name,
751 * and an optional argument of type signed or unsigned integer to request an
752 * explicit occurrence of the header. Note that in the event of a missing name,
753 * headers are considered from the first one. It does not stop on commas and
754 * returns full lines instead (useful for User-Agent or Date for example).
755 */
756static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
757{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200758 /* possible keywords: req.fhdr, res.fhdr */
759 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200760 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200761 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200762 struct http_hdr_ctx *ctx = smp->ctx.a[0];
763 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200764 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200765
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200766 if (!ctx) {
767 /* first call */
768 ctx = &static_http_hdr_ctx;
769 ctx->blk = NULL;
770 smp->ctx.a[0] = ctx;
771 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200772
Christopher Faulet623af932021-01-29 11:22:15 +0100773 if (args[0].type != ARGT_STR)
774 return 0;
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100775 name = ist2(args[0].data.str.area, args[0].data.str.data);
Willy Tarreau79e57332018-10-02 16:01:16 +0200776
Christopher Faulet623af932021-01-29 11:22:15 +0100777 if (args[1].type == ARGT_SINT)
778 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200779
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200780 if (!htx)
781 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200782
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200783 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
784 /* search for header from the beginning */
785 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200786
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200787 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
788 /* no explicit occurrence and single fetch => last header by default */
789 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200790
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200791 if (!occ)
792 /* prepare to report multiple occurrences for ACL fetches */
793 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200794
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200795 smp->data.type = SMP_T_STR;
796 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
797 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
798 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200799 smp->flags &= ~SMP_F_NOT_LAST;
800 return 0;
801}
802
803/* 6. Check on HTTP header count. The number of occurrences is returned.
804 * Accepts exactly 1 argument of type string. It does not stop on commas and
805 * returns full lines instead (useful for User-Agent or Date for example).
806 */
807static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
808{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200809 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
810 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200811 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200812 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200813 struct http_hdr_ctx ctx;
814 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200815 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200816
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200817 if (!htx)
818 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200819
Christopher Faulet623af932021-01-29 11:22:15 +0100820 if (args->type == ARGT_STR) {
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100821 name = ist2(args->data.str.area, args->data.str.data);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200822 } else {
Tim Duesterhus68a088d2021-02-28 16:11:37 +0100823 name = IST_NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200824 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200825
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200826 ctx.blk = NULL;
827 cnt = 0;
828 while (http_find_header(htx, name, &ctx, 1))
829 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200830 smp->data.type = SMP_T_SINT;
831 smp->data.u.sint = cnt;
832 smp->flags = SMP_F_VOL_HDR;
833 return 1;
834}
835
836static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
837{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200838 /* possible keywords: req.hdr_names, res.hdr_names */
839 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200840 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200841 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200842 struct buffer *temp;
843 char del = ',';
844
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200845 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200846
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200847 if (!htx)
848 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200849
Christopher Faulet623af932021-01-29 11:22:15 +0100850 if (args->type == ARGT_STR)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200851 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200852
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200853 temp = get_trash_chunk();
854 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
855 struct htx_blk *blk = htx_get_blk(htx, pos);
856 enum htx_blk_type type = htx_get_blk_type(blk);
857 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200858
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200859 if (type == HTX_BLK_EOH)
860 break;
861 if (type != HTX_BLK_HDR)
862 continue;
863 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200864
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200865 if (temp->data)
866 temp->area[temp->data++] = del;
867 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200868 }
869
870 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200871 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200872 smp->flags = SMP_F_VOL_HDR;
873 return 1;
874}
875
876/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
877 * Accepts an optional argument of type string containing the header field name,
878 * and an optional argument of type signed or unsigned integer to request an
879 * explicit occurrence of the header. Note that in the event of a missing name,
880 * headers are considered from the first one.
881 */
882static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
883{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200884 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
885 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200886 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200887 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200888 struct http_hdr_ctx *ctx = smp->ctx.a[0];
889 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200890 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200891
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200892 if (!ctx) {
893 /* first call */
894 ctx = &static_http_hdr_ctx;
895 ctx->blk = NULL;
896 smp->ctx.a[0] = ctx;
897 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200898
Christopher Faulet623af932021-01-29 11:22:15 +0100899 if (args[0].type != ARGT_STR)
900 return 0;
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100901 name = ist2(args[0].data.str.area, args[0].data.str.data);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200902
Christopher Faulet623af932021-01-29 11:22:15 +0100903 if (args[1].type == ARGT_SINT)
904 occ = args[1].data.sint;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200905
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200906 if (!htx)
907 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200908
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200909 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
910 /* search for header from the beginning */
911 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200912
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200913 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
914 /* no explicit occurrence and single fetch => last header by default */
915 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200916
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200917 if (!occ)
918 /* prepare to report multiple occurrences for ACL fetches */
919 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200920
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200921 smp->data.type = SMP_T_STR;
922 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
923 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
924 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200925
926 smp->flags &= ~SMP_F_NOT_LAST;
927 return 0;
928}
929
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200930/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
931 * the right channel. So instead of duplicating the code, we just change the
932 * keyword and then fallback on smp_fetch_hdr().
933 */
934static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
935{
936 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
937 return smp_fetch_hdr(args, smp, kw, private);
938}
939
Willy Tarreau79e57332018-10-02 16:01:16 +0200940/* 6. Check on HTTP header count. The number of occurrences is returned.
941 * Accepts exactly 1 argument of type string.
942 */
943static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
944{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200945 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
946 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200947 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200948 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200949 struct http_hdr_ctx ctx;
950 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200951 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200952
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200953 if (!htx)
954 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200955
Christopher Faulet623af932021-01-29 11:22:15 +0100956 if (args->type == ARGT_STR) {
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100957 name = ist2(args->data.str.area, args->data.str.data);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200958 } else {
Tim Duesterhus68a088d2021-02-28 16:11:37 +0100959 name = IST_NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200960 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200961
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200962 ctx.blk = NULL;
963 cnt = 0;
964 while (http_find_header(htx, name, &ctx, 0))
965 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200966
967 smp->data.type = SMP_T_SINT;
968 smp->data.u.sint = cnt;
969 smp->flags = SMP_F_VOL_HDR;
970 return 1;
971}
972
973/* Fetch an HTTP header's integer value. The integer value is returned. It
974 * takes a mandatory argument of type string and an optional one of type int
975 * to designate a specific occurrence. It returns an unsigned integer, which
976 * may or may not be appropriate for everything.
977 */
978static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
979{
980 int ret = smp_fetch_hdr(args, smp, kw, private);
981
982 if (ret > 0) {
983 smp->data.type = SMP_T_SINT;
984 smp->data.u.sint = strl2ic(smp->data.u.str.area,
985 smp->data.u.str.data);
986 }
987
988 return ret;
989}
990
991/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
992 * and an optional one of type int to designate a specific occurrence.
Willy Tarreau7b0e00d2021-03-25 14:12:29 +0100993 * It returns an IPv4 or IPv6 address. Addresses surrounded by invalid chars
994 * are rejected. However IPv4 addresses may be followed with a colon and a
995 * valid port number.
Willy Tarreau79e57332018-10-02 16:01:16 +0200996 */
997static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
998{
Tim Duesterhus5cd00872020-06-26 15:44:48 +0200999 struct buffer *temp = get_trash_chunk();
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001000 int ret, len;
1001 int port;
Willy Tarreau79e57332018-10-02 16:01:16 +02001002
1003 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001004 if (smp->data.u.str.data < temp->size - 1) {
1005 memcpy(temp->area, smp->data.u.str.area,
1006 smp->data.u.str.data);
1007 temp->area[smp->data.u.str.data] = '\0';
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001008 len = url2ipv4((char *) temp->area, &smp->data.u.ipv4);
Willy Tarreau645dc082021-03-31 11:41:36 +02001009 if (len > 0 && len == smp->data.u.str.data) {
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001010 /* plain IPv4 address */
1011 smp->data.type = SMP_T_IPV4;
1012 break;
1013 } else if (len > 0 && temp->area[len] == ':' &&
1014 strl2irc(temp->area + len + 1, smp->data.u.str.data - len - 1, &port) == 0 &&
1015 port >= 0 && port <= 65535) {
1016 /* IPv4 address suffixed with ':' followed by a valid port number */
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001017 smp->data.type = SMP_T_IPV4;
1018 break;
1019 } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1020 smp->data.type = SMP_T_IPV6;
1021 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001022 }
1023 }
1024
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001025 /* if the header doesn't match an IP address, fetch next one */
1026 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001027 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001028 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001029 return ret;
1030}
Willy Tarreau79e57332018-10-02 16:01:16 +02001031
Christopher Faulete720c322020-09-02 17:25:18 +02001032/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at the
1033 * first '/' after the possible hostname. It ends before the possible '?' except
1034 * for 'pathq' keyword.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001035 */
1036static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1037{
1038 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001039 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001040 struct htx_sl *sl;
1041 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02001042 struct http_uri_parser parser;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001043
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001044 if (!htx)
1045 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001046
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001047 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02001048 parser = http_uri_parser_init(htx_sl_req_uri(sl));
Christopher Faulete720c322020-09-02 17:25:18 +02001049
Yves Lafonb4d37082021-02-11 11:01:28 +01001050 if (kw[4] == 'q' && (kw[0] == 'p' || kw[0] == 'b')) // pathq or baseq
Amaury Denoyellec453f952021-07-06 11:40:12 +02001051 path = http_parse_path(&parser);
Christopher Faulete720c322020-09-02 17:25:18 +02001052 else
Amaury Denoyellec453f952021-07-06 11:40:12 +02001053 path = iststop(http_parse_path(&parser), '?');
Christopher Faulete720c322020-09-02 17:25:18 +02001054
Tim Duesterhused526372020-03-05 17:56:33 +01001055 if (!isttest(path))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001056 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001057
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001058 /* OK, we got the '/' ! */
1059 smp->data.type = SMP_T_STR;
1060 smp->data.u.str.area = path.ptr;
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001061 smp->data.u.str.data = path.len;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001062 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001063 return 1;
1064}
1065
1066/* This produces a concatenation of the first occurrence of the Host header
1067 * followed by the path component if it begins with a slash ('/'). This means
1068 * that '*' will not be added, resulting in exactly the first Host entry.
1069 * If no Host header is found, then the path is returned as-is. The returned
1070 * value is stored in the trash so it does not need to be marked constant.
1071 * The returned sample is of type string.
1072 */
1073static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1074{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001075 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001076 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001077 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001078 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001079 struct http_hdr_ctx ctx;
1080 struct ist path;
Amaury Denoyellec453f952021-07-06 11:40:12 +02001081 struct http_uri_parser parser;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001082
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001083 if (!htx)
1084 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001085
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001086 ctx.blk = NULL;
1087 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1088 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001089
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001090 /* OK we have the header value in ctx.value */
1091 temp = get_trash_chunk();
1092 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001093
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001094 /* now retrieve the path */
1095 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02001096 parser = http_uri_parser_init(htx_sl_req_uri(sl));
1097 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01001098 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001099 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001100
Yves Lafonb4d37082021-02-11 11:01:28 +01001101 if (kw[4] == 'q' && kw[0] == 'b') { // baseq
1102 len = path.len;
1103 } else {
1104 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1105 ;
1106 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001107
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001108 if (len && *(path.ptr) == '/')
1109 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001110 }
1111
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001112 smp->data.type = SMP_T_STR;
1113 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001114 smp->flags = SMP_F_VOL_1ST;
1115 return 1;
1116}
1117
1118/* This produces a 32-bit hash of the concatenation of the first occurrence of
1119 * the Host header followed by the path component if it begins with a slash ('/').
1120 * This means that '*' will not be added, resulting in exactly the first Host
1121 * entry. If no Host header is found, then the path is used. The resulting value
1122 * is hashed using the path hash followed by a full avalanche hash and provides a
1123 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1124 * high-traffic sites without having to store whole paths.
1125 */
1126static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1127{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001128 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001129 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001130 struct htx_sl *sl;
1131 struct http_hdr_ctx ctx;
1132 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001133 unsigned int hash = 0;
Amaury Denoyellec453f952021-07-06 11:40:12 +02001134 struct http_uri_parser parser;
Willy Tarreau79e57332018-10-02 16:01:16 +02001135
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001136 if (!htx)
1137 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001138
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001139 ctx.blk = NULL;
1140 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1141 /* OK we have the header value in ctx.value */
1142 while (ctx.value.len--)
1143 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001144 }
1145
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001146 /* now retrieve the path */
1147 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02001148 parser = http_uri_parser_init(htx_sl_req_uri(sl));
1149 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01001150 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001151 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001152
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001153 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1154 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001155
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001156 if (len && *(path.ptr) == '/') {
1157 while (len--)
1158 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001159 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001160 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001161
Willy Tarreau79e57332018-10-02 16:01:16 +02001162 hash = full_hash(hash);
1163
1164 smp->data.type = SMP_T_SINT;
1165 smp->data.u.sint = hash;
1166 smp->flags = SMP_F_VOL_1ST;
1167 return 1;
1168}
1169
1170/* This concatenates the source address with the 32-bit hash of the Host and
1171 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1172 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1173 * on the source address length. The path hash is stored before the address so
1174 * that in environments where IPv6 is insignificant, truncating the output to
1175 * 8 bytes would still work.
1176 */
1177static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1178{
1179 struct buffer *temp;
1180 struct connection *cli_conn = objt_conn(smp->sess->origin);
1181
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001182 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001183 return 0;
1184
1185 if (!smp_fetch_base32(args, smp, kw, private))
1186 return 0;
1187
1188 temp = get_trash_chunk();
1189 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1190 temp->data += sizeof(unsigned int);
1191
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001192 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001193 case AF_INET:
1194 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001195 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001196 4);
1197 temp->data += 4;
1198 break;
1199 case AF_INET6:
1200 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001201 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001202 16);
1203 temp->data += 16;
1204 break;
1205 default:
1206 return 0;
1207 }
1208
1209 smp->data.u.str = *temp;
1210 smp->data.type = SMP_T_BIN;
1211 return 1;
1212}
1213
1214/* Extracts the query string, which comes after the question mark '?'. If no
1215 * question mark is found, nothing is returned. Otherwise it returns a sample
1216 * of type string carrying the whole query string.
1217 */
1218static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1219{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001220 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001221 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001222 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001223 char *ptr, *end;
1224
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001225 if (!htx)
1226 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001227
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001228 sl = http_get_stline(htx);
1229 ptr = HTX_SL_REQ_UPTR(sl);
1230 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001231
1232 /* look up the '?' */
1233 do {
1234 if (ptr == end)
1235 return 0;
1236 } while (*ptr++ != '?');
1237
1238 smp->data.type = SMP_T_STR;
1239 smp->data.u.str.area = ptr;
1240 smp->data.u.str.data = end - ptr;
1241 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1242 return 1;
1243}
1244
1245static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1246{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001247 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001248 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001249
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001250 if (!htx)
1251 return 0;
1252 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001253 smp->data.u.sint = 1;
1254 return 1;
1255}
1256
1257/* return a valid test if the current request is the first one on the connection */
1258static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1259{
Willy Tarreau79512b62020-04-29 11:52:13 +02001260 if (!smp->strm)
1261 return 0;
1262
Willy Tarreau79e57332018-10-02 16:01:16 +02001263 smp->data.type = SMP_T_BOOL;
1264 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1265 return 1;
1266}
1267
Christopher Fauleta4063562019-08-02 11:51:37 +02001268/* Fetch the authentication method if there is an Authorization header. It
1269 * relies on get_http_auth()
1270 */
1271static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1272{
1273 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001274 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001275 struct http_txn *txn;
1276
1277 if (!htx)
1278 return 0;
1279
1280 txn = smp->strm->txn;
1281 if (!get_http_auth(smp, htx))
1282 return 0;
1283
1284 switch (txn->auth.method) {
1285 case HTTP_AUTH_BASIC:
1286 smp->data.u.str.area = "Basic";
1287 smp->data.u.str.data = 5;
1288 break;
1289 case HTTP_AUTH_DIGEST:
1290 /* Unexpected because not supported */
1291 smp->data.u.str.area = "Digest";
1292 smp->data.u.str.data = 6;
1293 break;
1294 default:
1295 return 0;
1296 }
1297
1298 smp->data.type = SMP_T_STR;
1299 smp->flags = SMP_F_CONST;
1300 return 1;
1301}
1302
1303/* Fetch the user supplied if there is an Authorization header. It relies on
1304 * get_http_auth()
1305 */
1306static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1307{
1308 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001309 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001310 struct http_txn *txn;
1311
1312 if (!htx)
1313 return 0;
1314
1315 txn = smp->strm->txn;
1316 if (!get_http_auth(smp, htx))
1317 return 0;
1318
1319 smp->data.type = SMP_T_STR;
1320 smp->data.u.str.area = txn->auth.user;
1321 smp->data.u.str.data = strlen(txn->auth.user);
1322 smp->flags = SMP_F_CONST;
1323 return 1;
1324}
1325
1326/* Fetch the password supplied if there is an Authorization header. It relies on
1327 * get_http_auth()
1328 */
1329static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1330{
1331 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001332 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001333 struct http_txn *txn;
1334
1335 if (!htx)
1336 return 0;
1337
1338 txn = smp->strm->txn;
1339 if (!get_http_auth(smp, htx))
1340 return 0;
1341
1342 smp->data.type = SMP_T_STR;
1343 smp->data.u.str.area = txn->auth.pass;
1344 smp->data.u.str.data = strlen(txn->auth.pass);
1345 smp->flags = SMP_F_CONST;
1346 return 1;
1347}
1348
Willy Tarreau79e57332018-10-02 16:01:16 +02001349/* Accepts exactly 1 argument of type userlist */
1350static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1351{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001352 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001353 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001354
Christopher Faulet623af932021-01-29 11:22:15 +01001355 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001356 return 0;
1357
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001358 if (!htx)
1359 return 0;
1360 if (!get_http_auth(smp, htx))
1361 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001362
1363 smp->data.type = SMP_T_BOOL;
1364 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001365 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001366 return 1;
1367}
1368
1369/* Accepts exactly 1 argument of type userlist */
1370static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1371{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001372 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001373 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001374
Christopher Faulet623af932021-01-29 11:22:15 +01001375 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001376 return 0;
1377
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001378 if (!htx)
1379 return 0;
1380 if (!get_http_auth(smp, htx))
1381 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001382
Willy Tarreau79e57332018-10-02 16:01:16 +02001383 /* if the user does not belong to the userlist or has a wrong password,
1384 * report that it unconditionally does not match. Otherwise we return
1385 * a string containing the username.
1386 */
1387 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1388 smp->strm->txn->auth.pass))
1389 return 0;
1390
1391 /* pat_match_auth() will need the user list */
1392 smp->ctx.a[0] = args->data.usr;
1393
1394 smp->data.type = SMP_T_STR;
1395 smp->flags = SMP_F_CONST;
1396 smp->data.u.str.area = smp->strm->txn->auth.user;
1397 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1398
1399 return 1;
1400}
1401
1402/* Fetch a captured HTTP request header. The index is the position of
1403 * the "capture" option in the configuration file
1404 */
1405static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1406{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001407 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001408 int idx;
1409
Christopher Faulet623af932021-01-29 11:22:15 +01001410 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001411 return 0;
1412
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001413 if (!smp->strm)
1414 return 0;
1415
1416 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001417 idx = args->data.sint;
1418
1419 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1420 return 0;
1421
1422 smp->data.type = SMP_T_STR;
1423 smp->flags |= SMP_F_CONST;
1424 smp->data.u.str.area = smp->strm->req_cap[idx];
1425 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1426
1427 return 1;
1428}
1429
1430/* Fetch a captured HTTP response header. The index is the position of
1431 * the "capture" option in the configuration file
1432 */
1433static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1434{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001435 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001436 int idx;
1437
Christopher Faulet623af932021-01-29 11:22:15 +01001438 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001439 return 0;
1440
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001441 if (!smp->strm)
1442 return 0;
1443
1444 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001445 idx = args->data.sint;
1446
1447 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1448 return 0;
1449
1450 smp->data.type = SMP_T_STR;
1451 smp->flags |= SMP_F_CONST;
1452 smp->data.u.str.area = smp->strm->res_cap[idx];
1453 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1454
1455 return 1;
1456}
1457
1458/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1459static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1460{
1461 struct buffer *temp;
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001462 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001463 char *ptr;
1464
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001465 if (!smp->strm)
1466 return 0;
1467
1468 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001469 if (!txn || !txn->uri)
1470 return 0;
1471
1472 ptr = txn->uri;
1473
1474 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1475 ptr++;
1476
1477 temp = get_trash_chunk();
1478 temp->area = txn->uri;
1479 temp->data = ptr - txn->uri;
1480 smp->data.u.str = *temp;
1481 smp->data.type = SMP_T_STR;
1482 smp->flags = SMP_F_CONST;
1483
1484 return 1;
1485
1486}
1487
1488/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1489static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1490{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001491 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001492 struct ist path;
1493 const char *ptr;
Amaury Denoyellec453f952021-07-06 11:40:12 +02001494 struct http_uri_parser parser;
Willy Tarreau79e57332018-10-02 16:01:16 +02001495
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
Amaury Denoyellec453f952021-07-06 11:40:12 +02001517 parser = http_uri_parser_init(path);
1518 path = http_parse_path(&parser);
Tim Duesterhused526372020-03-05 17:56:33 +01001519 if (!isttest(path))
Willy Tarreau79e57332018-10-02 16:01:16 +02001520 return 0;
1521
1522 smp->data.u.str.area = path.ptr;
1523 smp->data.u.str.data = path.len;
1524 smp->data.type = SMP_T_STR;
1525 smp->flags = SMP_F_CONST;
1526
1527 return 1;
1528}
1529
1530/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1531 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1532 */
1533static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1534{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001535 struct http_txn *txn;
1536
1537 if (!smp->strm)
1538 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001539
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001540 txn = smp->strm->txn;
Christopher Faulet09f88362021-04-01 16:00:29 +02001541 if (!txn || txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001542 return 0;
1543
1544 if (txn->req.flags & HTTP_MSGF_VER_11)
1545 smp->data.u.str.area = "HTTP/1.1";
1546 else
1547 smp->data.u.str.area = "HTTP/1.0";
1548
1549 smp->data.u.str.data = 8;
1550 smp->data.type = SMP_T_STR;
1551 smp->flags = SMP_F_CONST;
1552 return 1;
1553
1554}
1555
1556/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1557 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1558 */
1559static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1560{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001561 struct http_txn *txn;
1562
1563 if (!smp->strm)
1564 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001565
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001566 txn = smp->strm->txn;
Christopher Faulet09f88362021-04-01 16:00:29 +02001567 if (!txn || txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001568 return 0;
1569
1570 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1571 smp->data.u.str.area = "HTTP/1.1";
1572 else
1573 smp->data.u.str.area = "HTTP/1.0";
1574
1575 smp->data.u.str.data = 8;
1576 smp->data.type = SMP_T_STR;
1577 smp->flags = SMP_F_CONST;
1578 return 1;
1579
1580}
1581
1582/* Iterate over all cookies present in a message. The context is stored in
1583 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1584 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1585 * the direction, multiple cookies may be parsed on the same line or not.
Maciej Zdebdea7c202020-11-13 09:38:06 +00001586 * If provided, the searched cookie name is in args, in args->data.str. If
1587 * the input options indicate that no iterating is desired, then only last
1588 * value is fetched if any. If no cookie name is provided, the first cookie
1589 * value found is fetched. The returned sample is of type CSTR. Can be used
1590 * to parse cookies in other files.
Willy Tarreau79e57332018-10-02 16:01:16 +02001591 */
1592static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1593{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001594 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1595 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001596 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001597 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001598 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1599 struct ist hdr;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001600 char *cook = NULL;
1601 size_t cook_l = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001602 int found = 0;
1603
Christopher Faulet623af932021-01-29 11:22:15 +01001604 if (args->type == ARGT_STR) {
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001605 cook = args->data.str.area;
1606 cook_l = args->data.str.data;
1607 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001608
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001609 if (!ctx) {
1610 /* first call */
1611 ctx = &static_http_hdr_ctx;
1612 ctx->blk = NULL;
1613 smp->ctx.a[2] = ctx;
1614 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001615
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001616 if (!htx)
1617 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001618
Christopher Faulet16032ab2020-04-30 11:30:00 +02001619 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001620
Maciej Zdebdea7c202020-11-13 09:38:06 +00001621 /* OK so basically here, either we want only one value or we want to
1622 * iterate over all of them and we fetch the next one. In this last case
1623 * SMP_OPT_ITERATE option is set.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001624 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001625
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001626 if (!(smp->flags & SMP_F_NOT_LAST)) {
1627 /* search for the header from the beginning, we must first initialize
1628 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001629 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001630 smp->ctx.a[0] = NULL;
1631 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001632 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001633
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001634 smp->flags |= SMP_F_VOL_HDR;
1635 while (1) {
1636 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1637 if (!smp->ctx.a[0]) {
1638 if (!http_find_header(htx, hdr, ctx, 0))
1639 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001640
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001641 if (ctx->value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001642 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001643
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001644 smp->ctx.a[0] = ctx->value.ptr;
1645 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001646 }
1647
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001648 smp->data.type = SMP_T_STR;
1649 smp->flags |= SMP_F_CONST;
1650 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001651 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001652 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1653 &smp->data.u.str.area,
1654 &smp->data.u.str.data);
1655 if (smp->ctx.a[0]) {
1656 found = 1;
Maciej Zdebdea7c202020-11-13 09:38:06 +00001657 if (smp->opt & SMP_OPT_ITERATE) {
1658 /* iterate on cookie value */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001659 smp->flags |= SMP_F_NOT_LAST;
1660 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001661 }
Maciej Zdebdea7c202020-11-13 09:38:06 +00001662 if (args->data.str.data == 0) {
1663 /* No cookie name, first occurrence returned */
1664 break;
1665 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001666 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001667 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001668 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001669
Willy Tarreau79e57332018-10-02 16:01:16 +02001670 /* all cookie headers and values were scanned. If we're looking for the
1671 * last occurrence, we may return it now.
1672 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001673 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001674 smp->flags &= ~SMP_F_NOT_LAST;
1675 return found;
1676}
1677
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001678/* Same than smp_fetch_cookie() but only relies on the sample direction to
1679 * choose the right channel. So instead of duplicating the code, we just change
1680 * the keyword and then fallback on smp_fetch_cookie().
1681 */
1682static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1683{
1684 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1685 return smp_fetch_cookie(args, smp, kw, private);
1686}
1687
Willy Tarreau79e57332018-10-02 16:01:16 +02001688/* Iterate over all cookies present in a request to count how many occurrences
1689 * match the name in args and args->data.str.len. If <multi> is non-null, then
1690 * multiple cookies may be parsed on the same line. The returned sample is of
1691 * type UINT. Accepts exactly 1 argument of type string.
1692 */
1693static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1694{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001695 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1696 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001697 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001698 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001699 struct http_hdr_ctx ctx;
1700 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001701 char *val_beg, *val_end;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001702 char *cook = NULL;
1703 size_t cook_l = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001704 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001705
Christopher Faulet623af932021-01-29 11:22:15 +01001706 if (args->type == ARGT_STR){
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001707 cook = args->data.str.area;
1708 cook_l = args->data.str.data;
1709 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001710
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001711 if (!htx)
1712 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001713
Christopher Faulet16032ab2020-04-30 11:30:00 +02001714 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001715
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001716 val_end = val_beg = NULL;
1717 ctx.blk = NULL;
1718 cnt = 0;
1719 while (1) {
1720 /* Note: val_beg == NULL every time we need to fetch a new header */
1721 if (!val_beg) {
1722 if (!http_find_header(htx, hdr, &ctx, 0))
1723 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001724
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001725 if (ctx.value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001726 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001727
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001728 val_beg = ctx.value.ptr;
1729 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001730 }
1731
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001732 smp->data.type = SMP_T_STR;
1733 smp->flags |= SMP_F_CONST;
1734 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001735 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001736 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1737 &smp->data.u.str.area,
1738 &smp->data.u.str.data))) {
1739 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001740 }
1741 }
1742
1743 smp->data.type = SMP_T_SINT;
1744 smp->data.u.sint = cnt;
1745 smp->flags |= SMP_F_VOL_HDR;
1746 return 1;
1747}
1748
1749/* Fetch an cookie's integer value. The integer value is returned. It
1750 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1751 */
1752static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1753{
1754 int ret = smp_fetch_cookie(args, smp, kw, private);
1755
1756 if (ret > 0) {
1757 smp->data.type = SMP_T_SINT;
1758 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1759 smp->data.u.str.data);
1760 }
1761
1762 return ret;
1763}
1764
1765/************************************************************************/
1766/* The code below is dedicated to sample fetches */
1767/************************************************************************/
1768
1769/* This scans a URL-encoded query string. It takes an optionally wrapping
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001770 * string whose first contiguous chunk has its beginning in ctx->a[0] and end
Willy Tarreau79e57332018-10-02 16:01:16 +02001771 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1772 * pointers are updated for next iteration before leaving.
1773 */
1774static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1775{
1776 const char *vstart, *vend;
1777 struct buffer *temp;
1778 const char **chunks = (const char **)smp->ctx.a;
1779
1780 if (!http_find_next_url_param(chunks, name, name_len,
1781 &vstart, &vend, delim))
1782 return 0;
1783
1784 /* Create sample. If the value is contiguous, return the pointer as CONST,
1785 * if the value is wrapped, copy-it in a buffer.
1786 */
1787 smp->data.type = SMP_T_STR;
1788 if (chunks[2] &&
1789 vstart >= chunks[0] && vstart <= chunks[1] &&
1790 vend >= chunks[2] && vend <= chunks[3]) {
1791 /* Wrapped case. */
1792 temp = get_trash_chunk();
1793 memcpy(temp->area, vstart, chunks[1] - vstart);
1794 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1795 vend - chunks[2]);
1796 smp->data.u.str.area = temp->area;
1797 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1798 } else {
1799 /* Contiguous case. */
1800 smp->data.u.str.area = (char *)vstart;
1801 smp->data.u.str.data = vend - vstart;
1802 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1803 }
1804
1805 /* Update context, check wrapping. */
1806 chunks[0] = vend;
1807 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1808 chunks[1] = chunks[3];
1809 chunks[2] = NULL;
1810 }
1811
1812 if (chunks[0] < chunks[1])
1813 smp->flags |= SMP_F_NOT_LAST;
1814
1815 return 1;
1816}
1817
1818/* This function iterates over each parameter of the query string. It uses
1819 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1820 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1821 * An optional parameter name is passed in args[0], otherwise any parameter is
1822 * considered. It supports an optional delimiter argument for the beginning of
1823 * the string in args[1], which defaults to "?".
1824 */
1825static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1826{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001827 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001828 char delim = '?';
1829 const char *name;
1830 int name_len;
1831
Christopher Faulet623af932021-01-29 11:22:15 +01001832 if ((args[0].type && args[0].type != ARGT_STR) ||
Willy Tarreau79e57332018-10-02 16:01:16 +02001833 (args[1].type && args[1].type != ARGT_STR))
1834 return 0;
1835
1836 name = "";
1837 name_len = 0;
1838 if (args->type == ARGT_STR) {
1839 name = args->data.str.area;
1840 name_len = args->data.str.data;
1841 }
1842
1843 if (args[1].type)
1844 delim = *args[1].data.str.area;
1845
1846 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001847 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001848 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001849
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001850 if (!htx)
1851 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001852
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001853 sl = http_get_stline(htx);
1854 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1855 if (!smp->ctx.a[0])
1856 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001857
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001858 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001859
1860 /* Assume that the context is filled with NULL pointer
1861 * before the first call.
1862 * smp->ctx.a[2] = NULL;
1863 * smp->ctx.a[3] = NULL;
1864 */
1865 }
1866
1867 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1868}
1869
1870/* This function iterates over each parameter of the body. This requires
1871 * that the body has been waited for using http-buffer-request. It uses
1872 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001873 * contiguous part of the body, and optionally ctx->a[2..3] to reference the
Willy Tarreau79e57332018-10-02 16:01:16 +02001874 * optional second part if the body wraps at the end of the buffer. An optional
1875 * parameter name is passed in args[0], otherwise any parameter is considered.
1876 */
1877static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1878{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001879 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001880 const char *name;
1881 int name_len;
1882
Christopher Faulet623af932021-01-29 11:22:15 +01001883 if (args[0].type && args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001884 return 0;
1885
1886 name = "";
1887 name_len = 0;
1888 if (args[0].type == ARGT_STR) {
1889 name = args[0].data.str.area;
1890 name_len = args[0].data.str.data;
1891 }
1892
1893 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulete596d182020-05-05 17:46:34 +02001894 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001895 struct buffer *temp;
1896 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001897
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001898 if (!htx)
1899 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001900
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001901 temp = get_trash_chunk();
1902 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1903 struct htx_blk *blk = htx_get_blk(htx, pos);
1904 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001905
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001906 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001907 break;
1908 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001909 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001910 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001911 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001912 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001913
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001914 smp->ctx.a[0] = temp->area;
1915 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001916
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001917 /* Assume that the context is filled with NULL pointer
1918 * before the first call.
1919 * smp->ctx.a[2] = NULL;
1920 * smp->ctx.a[3] = NULL;
1921 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001922
Willy Tarreau79e57332018-10-02 16:01:16 +02001923 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001924
Willy Tarreau79e57332018-10-02 16:01:16 +02001925 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1926}
1927
1928/* Return the signed integer value for the specified url parameter (see url_param
1929 * above).
1930 */
1931static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1932{
1933 int ret = smp_fetch_url_param(args, smp, kw, private);
1934
1935 if (ret > 0) {
1936 smp->data.type = SMP_T_SINT;
1937 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1938 smp->data.u.str.data);
1939 }
1940
1941 return ret;
1942}
1943
1944/* This produces a 32-bit hash of the concatenation of the first occurrence of
1945 * the Host header followed by the path component if it begins with a slash ('/').
1946 * This means that '*' will not be added, resulting in exactly the first Host
1947 * entry. If no Host header is found, then the path is used. The resulting value
1948 * is hashed using the url hash followed by a full avalanche hash and provides a
1949 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1950 * high-traffic sites without having to store whole paths.
1951 * this differs from the base32 functions in that it includes the url parameters
1952 * as well as the path
1953 */
1954static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1955{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001956 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001957 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001958 struct http_hdr_ctx ctx;
1959 struct htx_sl *sl;
1960 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001961 unsigned int hash = 0;
Amaury Denoyellec453f952021-07-06 11:40:12 +02001962 struct http_uri_parser parser;
Willy Tarreau79e57332018-10-02 16:01:16 +02001963
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001964 if (!htx)
1965 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001966
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001967 ctx.blk = NULL;
1968 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1969 /* OK we have the header value in ctx.value */
1970 while (ctx.value.len--)
1971 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001972 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001973
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001974 /* now retrieve the path */
1975 sl = http_get_stline(htx);
Amaury Denoyellec453f952021-07-06 11:40:12 +02001976 parser = http_uri_parser_init(htx_sl_req_uri(sl));
1977 path = http_parse_path(&parser);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001978 if (path.len && *(path.ptr) == '/') {
1979 while (path.len--)
1980 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001981 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001982
Willy Tarreau79e57332018-10-02 16:01:16 +02001983 hash = full_hash(hash);
1984
1985 smp->data.type = SMP_T_SINT;
1986 smp->data.u.sint = hash;
1987 smp->flags = SMP_F_VOL_1ST;
1988 return 1;
1989}
1990
1991/* This concatenates the source address with the 32-bit hash of the Host and
1992 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1993 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1994 * on the source address length. The URL hash is stored before the address so
1995 * that in environments where IPv6 is insignificant, truncating the output to
1996 * 8 bytes would still work.
1997 */
1998static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1999{
2000 struct buffer *temp;
2001 struct connection *cli_conn = objt_conn(smp->sess->origin);
2002
Willy Tarreaucd7ca792019-07-17 16:57:03 +02002003 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02002004 return 0;
2005
2006 if (!smp_fetch_url32(args, smp, kw, private))
2007 return 0;
2008
2009 temp = get_trash_chunk();
2010 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2011 temp->data += sizeof(unsigned int);
2012
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002013 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02002014 case AF_INET:
2015 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002016 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002017 4);
2018 temp->data += 4;
2019 break;
2020 case AF_INET6:
2021 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002022 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002023 16);
2024 temp->data += 16;
2025 break;
2026 default:
2027 return 0;
2028 }
2029
2030 smp->data.u.str = *temp;
2031 smp->data.type = SMP_T_BIN;
2032 return 1;
2033}
2034
2035/************************************************************************/
2036/* Other utility functions */
2037/************************************************************************/
2038
2039/* This function is used to validate the arguments passed to any "hdr" fetch
2040 * keyword. These keywords support an optional positive or negative occurrence
2041 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2042 * is assumed that the types are already the correct ones. Returns 0 on error,
2043 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2044 * error message in case of error, that the caller is responsible for freeing.
2045 * The initial location must either be freeable or NULL.
2046 * Note: this function's pointer is checked from Lua.
2047 */
2048int val_hdr(struct arg *arg, char **err_msg)
2049{
2050 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2051 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2052 return 0;
2053 }
2054 return 1;
2055}
2056
2057/************************************************************************/
2058/* All supported sample fetch keywords must be declared here. */
2059/************************************************************************/
2060
2061/* Note: must not be declared <const> as its list will be overwritten */
2062static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2063 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2064 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2065 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Yves Lafonb4d37082021-02-11 11:01:28 +01002066 { "baseq", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002067
2068 /* capture are allocated and are permanent in the stream */
2069 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2070
2071 /* retrieve these captures from the HTTP logs */
2072 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2073 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2074 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2075
2076 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2077 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2078
2079 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2080 * are only here to match the ACL's name, are request-only and are used
2081 * for ACL compatibility only.
2082 */
2083 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002084 { "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 +02002085 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2086 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2087
2088 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2089 * only here to match the ACL's name, are request-only and are used for
2090 * ACL compatibility only.
2091 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002092 { "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 +02002093 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2094 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2095 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2096
Christopher Fauleta4063562019-08-02 11:51:37 +02002097 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2098 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2099 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002100 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2101 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2102 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2103 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2104 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Faulete720c322020-09-02 17:25:18 +02002105 { "pathq", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002106 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2107
2108 /* HTTP protocol on the request path */
2109 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2110 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2111
2112 /* HTTP version on the request path */
2113 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2114 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2115
2116 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2117 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2118 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2119 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2120
2121 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2122 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2123
2124 /* HTTP version on the response path */
2125 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2126 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2127
Christopher Faulete596d182020-05-05 17:46:34 +02002128 { "res.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2129 { "res.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2130 { "res.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2131
2132 { "res.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2133 { "res.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2134
Willy Tarreau79e57332018-10-02 16:01:16 +02002135 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2136 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2137 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2138 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2139
2140 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2141 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2142 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2143 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2144 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2145 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2146 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2147
2148 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2149 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2150 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2151 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2152
2153 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2154 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2155 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2156 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2157 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2158 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2159 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2160
2161 /* scook is valid only on the response and is used for ACL compatibility */
2162 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2163 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2164 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002165
2166 /* shdr is valid only on the response and is used for ACL compatibility */
2167 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2168 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2169 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2170 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2171
2172 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2173 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2174 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2175 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2176 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2177 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2178 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2179 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2180 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2181 { "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 +02002182
Willy Tarreau79e57332018-10-02 16:01:16 +02002183 { /* END */ },
2184}};
2185
Willy Tarreau0108d902018-11-25 19:14:37 +01002186INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002187
2188/*
2189 * Local variables:
2190 * c-indent-level: 8
2191 * c-basic-offset: 8
2192 * End:
2193 */