blob: 3d627a926a0efa87062e5e7eaef785ac862b7e42 [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
211 if (!s->txn) {
212 if (unlikely(!http_alloc_txn(s)))
213 return NULL; /* not enough memory */
214 http_init_txn(s);
215 txn = s->txn;
216 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200217 txn = s->txn;
218 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200219
Christopher Fauleteca88542019-04-03 10:12:42 +0200220 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200221 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200222
Christopher Faulet89dc4992019-04-17 12:02:59 +0200223 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
224 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200225
Christopher Faulet89dc4992019-04-17 12:02:59 +0200226 if (msg->msg_state < HTTP_MSG_BODY) {
227 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200228 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200229 /* Parsing is done by the mux, just wait */
230 smp->flags |= SMP_F_MAY_CHANGE;
231 return NULL;
232 }
233 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200234 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200235 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200236 /* The start-line was already forwarded, it is too late to fetch anything */
237 return NULL;
238 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200239 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200240 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200241 struct buffer *buf;
242 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200243 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200244 union h1_sl h1sl;
245 unsigned int flags = HTX_FL_NONE;
246 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200247
Christopher Faulet89dc4992019-04-17 12:02:59 +0200248 /* no HTTP fetch on the response in TCP mode */
249 if (chn->flags & CF_ISRESP)
250 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200251
Christopher Faulet89dc4992019-04-17 12:02:59 +0200252 /* Now we are working on the request only */
253 buf = &chn->buf;
254 if (b_head(buf) + b_data(buf) > b_wrap(buf))
255 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200256
Christopher Faulet89dc4992019-04-17 12:02:59 +0200257 h1m_init_req(&h1m);
258 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
259 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
260 if (ret <= 0) {
261 /* Invalid or too big*/
262 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200263 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100264
Christopher Faulet89dc4992019-04-17 12:02:59 +0200265 /* wait for a full request */
266 smp->flags |= SMP_F_MAY_CHANGE;
267 return NULL;
268 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100269
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500270 /* OK we just got a valid HTTP message. We have to convert it
Christopher Faulet89dc4992019-04-17 12:02:59 +0200271 * into an HTX message.
272 */
273 if (unlikely(h1sl.rq.v.len == 0)) {
274 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
275 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200276 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200277 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200278 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200279
280 /* Set HTX start-line flags */
281 if (h1m.flags & H1_MF_VER_11)
282 flags |= HTX_SL_F_VER_11;
283 if (h1m.flags & H1_MF_XFER_ENC)
284 flags |= HTX_SL_F_XFER_ENC;
285 flags |= HTX_SL_F_XFER_LEN;
286 if (h1m.flags & H1_MF_CHNK)
287 flags |= HTX_SL_F_CHNK;
288 else if (h1m.flags & H1_MF_CLEN)
289 flags |= HTX_SL_F_CLEN;
290
Richard Russo458eafb2019-07-31 11:45:56 -0700291 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200292 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
293 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200294 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200295 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200296 }
297
298 /* OK we just got a valid HTTP message. If not already done by
299 * HTTP analyzers, we have some minor preparation to perform so
300 * that further checks can rely on HTTP tests.
301 */
302 if (sl && msg->msg_state < HTTP_MSG_BODY) {
303 if (!(chn->flags & CF_ISRESP)) {
304 txn->meth = sl->info.req.meth;
305 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
306 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200307 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200308 else
309 txn->status = sl->info.res.status;
310 if (sl->flags & HTX_SL_F_VER_11)
311 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200312 }
313
314 /* everything's OK */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200315 end:
Christopher Fauletef453ed2018-10-24 21:39:27 +0200316 return htx;
317}
318
Willy Tarreau79e57332018-10-02 16:01:16 +0200319/* This function fetches the method of current HTTP request and stores
320 * it in the global pattern struct as a chunk. There are two possibilities :
321 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
322 * in <len> and <ptr> is NULL ;
323 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
324 * <len> to its length.
325 * This is intended to be used with pat_match_meth() only.
326 */
327static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
328{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200329 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200330 struct http_txn *txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200331 int meth;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200332
Willy Tarreaua6d98792020-08-12 14:04:52 +0200333 txn = smp->strm->txn;
334 if (!txn)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200335 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200336
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200337 meth = txn->meth;
338 smp->data.type = SMP_T_METH;
339 smp->data.u.meth.meth = meth;
340 if (meth == HTTP_METH_OTHER) {
Willy Tarreaua6d98792020-08-12 14:04:52 +0200341 struct htx *htx;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200342 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200343
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200344 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
345 /* ensure the indexes are not affected */
346 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200347 }
Willy Tarreaua6d98792020-08-12 14:04:52 +0200348
349 htx = smp_prefetch_htx(smp, chn, NULL, 0);
350 if (!htx)
351 return 0;
352
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200353 sl = http_get_stline(htx);
354 smp->flags |= SMP_F_CONST;
355 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
356 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200357 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200358 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200359 return 1;
360}
361
362static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
363{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200364 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200365 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200366 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200367 char *ptr;
368 int len;
369
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200370 if (!htx)
371 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200372
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200373 sl = http_get_stline(htx);
374 len = HTX_SL_REQ_VLEN(sl);
375 ptr = HTX_SL_REQ_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200376
377 while ((len-- > 0) && (*ptr++ != '/'));
378 if (len <= 0)
379 return 0;
380
381 smp->data.type = SMP_T_STR;
382 smp->data.u.str.area = ptr;
383 smp->data.u.str.data = len;
384
385 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
386 return 1;
387}
388
389static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
390{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200391 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200392 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200393 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200394 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200395 char *ptr;
396 int len;
397
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200398 if (!htx)
399 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200400
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200401 sl = http_get_stline(htx);
402 len = HTX_SL_RES_VLEN(sl);
403 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200404
405 while ((len-- > 0) && (*ptr++ != '/'));
406 if (len <= 0)
407 return 0;
408
409 smp->data.type = SMP_T_STR;
410 smp->data.u.str.area = ptr;
411 smp->data.u.str.data = len;
412
413 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
414 return 1;
415}
416
417/* 3. Check on Status Code. We manipulate integers here. */
418static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
419{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200420 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200421 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200422 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200423 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200424 char *ptr;
425 int len;
426
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200427 if (!htx)
428 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200429
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200430 sl = http_get_stline(htx);
431 len = HTX_SL_RES_CLEN(sl);
432 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200433
434 smp->data.type = SMP_T_SINT;
435 smp->data.u.sint = __strl2ui(ptr, len);
436 smp->flags = SMP_F_VOL_1ST;
437 return 1;
438}
439
440static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
441{
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100442 struct ist unique_id;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100443
Willy Tarreau79e57332018-10-02 16:01:16 +0200444 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
445 return 0;
446
Willy Tarreaua1062a42020-04-29 11:50:38 +0200447 if (!smp->strm)
448 return 0;
449
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100450 unique_id = stream_generate_unique_id(smp->strm, &smp->sess->fe->format_unique_id);
451 if (!isttest(unique_id))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100452 return 0;
453
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100454 smp->data.u.str.area = smp->strm->unique_id.ptr;
455 smp->data.u.str.data = smp->strm->unique_id.len;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100456 smp->data.type = SMP_T_STR;
Willy Tarreau79e57332018-10-02 16:01:16 +0200457 smp->flags = SMP_F_CONST;
458 return 1;
459}
460
461/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800462 * empty line which separes headers from the body. This is useful
463 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200464 */
465static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
466{
Christopher Faulete596d182020-05-05 17:46:34 +0200467 /* possible keywords: req.hdrs, res.hdrs */
468 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200469 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200470 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200471 struct buffer *temp;
472 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200473
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200474 if (!htx)
475 return 0;
476 temp = get_trash_chunk();
477 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
478 struct htx_blk *blk = htx_get_blk(htx, pos);
479 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200480
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200481 if (type == HTX_BLK_HDR) {
482 struct ist n = htx_get_blk_name(htx, blk);
483 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200484
Christopher Faulet53a899b2019-10-08 16:38:42 +0200485 if (!h1_format_htx_hdr(n, v, temp))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200486 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200487 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200488 else if (type == HTX_BLK_EOH) {
489 if (!chunk_memcat(temp, "\r\n", 2))
490 return 0;
491 break;
492 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200493 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200494 smp->data.type = SMP_T_STR;
495 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200496 return 1;
497}
498
499/* Returns the header request in a length/value encoded format.
500 * This is useful for exchanges with the SPOE.
501 *
502 * A "length value" is a multibyte code encoding numbers. It uses the
503 * SPOE format. The encoding is the following:
504 *
505 * Each couple "header name" / "header value" is composed
506 * like this:
507 * "length value" "header name bytes"
508 * "length value" "header value bytes"
509 * When the last header is reached, the header name and the header
510 * value are empty. Their length are 0
511 */
512static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
513{
Christopher Faulete596d182020-05-05 17:46:34 +0200514 /* possible keywords: req.hdrs_bin, res.hdrs_bin */
515 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200516 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200517 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200518 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200519 char *p, *end;
520 int32_t pos;
521 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200522
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200523 if (!htx)
524 return 0;
525 temp = get_trash_chunk();
526 p = temp->area;
527 end = temp->area + temp->size;
528 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
529 struct htx_blk *blk = htx_get_blk(htx, pos);
530 enum htx_blk_type type = htx_get_blk_type(blk);
531 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200532
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200533 if (type == HTX_BLK_HDR) {
534 n = htx_get_blk_name(htx,blk);
535 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200536
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200537 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200538 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200539 if (ret == -1)
540 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200541 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200542 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200543 memcpy(p, n.ptr, n.len);
544 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200545
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200546 /* encode the header value. */
547 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200548 if (ret == -1)
549 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200550 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200551 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200552 memcpy(p, v.ptr, v.len);
553 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200554
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200555 }
556 else if (type == HTX_BLK_EOH) {
557 /* encode the end of the header list with empty
558 * header name and header value.
559 */
560 ret = encode_varint(0, &p, end);
561 if (ret == -1)
562 return 0;
563 ret = encode_varint(0, &p, end);
564 if (ret == -1)
565 return 0;
566 break;
567 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200568 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200569
570 /* Initialise sample data which will be filled. */
571 smp->data.type = SMP_T_BIN;
572 smp->data.u.str.area = temp->area;
573 smp->data.u.str.data = p - temp->area;
574 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200575 return 1;
576}
577
578/* returns the longest available part of the body. This requires that the body
579 * has been waited for using http-buffer-request.
580 */
581static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
582{
Christopher Faulete596d182020-05-05 17:46:34 +0200583 /* possible keywords: req.body, res.body */
584 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200585 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200586 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200587 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200588 int32_t pos;
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100589 int finished = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200590
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200591 if (!htx)
592 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200593
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200594 temp = get_trash_chunk();
595 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
596 struct htx_blk *blk = htx_get_blk(htx, pos);
597 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200598
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100599 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) {
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100600 finished = 1;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200601 break;
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100602 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200603 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +0200604 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200605 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200606 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200607 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200608
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200609 smp->data.type = SMP_T_BIN;
610 smp->data.u.str = *temp;
611 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200612
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100613 if (!finished && (check || (chn && !channel_full(chn, global.tune.maxrewrite) &&
614 !(chn->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)))))
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200615 smp->flags |= SMP_F_MAY_CHANGE;
616
Willy Tarreau79e57332018-10-02 16:01:16 +0200617 return 1;
618}
619
620
621/* returns the available length of the body. This requires that the body
622 * has been waited for using http-buffer-request.
623 */
624static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
625{
Christopher Faulete596d182020-05-05 17:46:34 +0200626 /* possible keywords: req.body_len, res.body_len */
627 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200628 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200629 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200630 int32_t pos;
631 unsigned long long len = 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100632
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200633 if (!htx)
634 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100635
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200636 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
637 struct htx_blk *blk = htx_get_blk(htx, pos);
638 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100639
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100640 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200641 break;
642 if (type == HTX_BLK_DATA)
643 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200644 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200645
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200646 smp->data.type = SMP_T_SINT;
647 smp->data.u.sint = len;
648 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200649 return 1;
650}
651
652
653/* returns the advertised length of the body, or the advertised size of the
654 * chunks available in the buffer. This requires that the body has been waited
655 * for using http-buffer-request.
656 */
657static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
658{
Christopher Faulete596d182020-05-05 17:46:34 +0200659 /* possible keywords: req.body_size, res.body_size */
660 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200661 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200662 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200663 int32_t pos;
664 unsigned long long len = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200665
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200666 if (!htx)
667 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100668
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200669 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
670 struct htx_blk *blk = htx_get_blk(htx, pos);
671 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100672
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100673 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200674 break;
675 if (type == HTX_BLK_DATA)
676 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200677 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200678 if (htx->extra != ULLONG_MAX)
679 len += htx->extra;
Willy Tarreau79e57332018-10-02 16:01:16 +0200680
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200681 smp->data.type = SMP_T_SINT;
682 smp->data.u.sint = len;
683 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200684 return 1;
685}
686
687
688/* 4. Check on URL/URI. A pointer to the URI is stored. */
689static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
690{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200691 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200692 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200693 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200694
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200695 if (!htx)
696 return 0;
697 sl = http_get_stline(htx);
698 smp->data.type = SMP_T_STR;
699 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
700 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
701 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200702 return 1;
703}
704
705static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
706{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200707 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200708 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200709 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200710 struct sockaddr_storage addr;
711
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200712 if (!htx)
713 return 0;
714 sl = http_get_stline(htx);
715 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +0200716
Willy Tarreau79e57332018-10-02 16:01:16 +0200717 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
718 return 0;
719
720 smp->data.type = SMP_T_IPV4;
721 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
722 smp->flags = 0;
723 return 1;
724}
725
726static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
727{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200728 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200729 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200730 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200731 struct sockaddr_storage addr;
732
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200733 if (!htx)
734 return 0;
735 sl = http_get_stline(htx);
736 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200737
Willy Tarreau79e57332018-10-02 16:01:16 +0200738 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
739 return 0;
740
741 smp->data.type = SMP_T_SINT;
742 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
743 smp->flags = 0;
744 return 1;
745}
746
747/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
748 * Accepts an optional argument of type string containing the header field name,
749 * and an optional argument of type signed or unsigned integer to request an
750 * explicit occurrence of the header. Note that in the event of a missing name,
751 * headers are considered from the first one. It does not stop on commas and
752 * returns full lines instead (useful for User-Agent or Date for example).
753 */
754static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
755{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200756 /* possible keywords: req.fhdr, res.fhdr */
757 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200758 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200759 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200760 struct http_hdr_ctx *ctx = smp->ctx.a[0];
761 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200762 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200763
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200764 if (!ctx) {
765 /* first call */
766 ctx = &static_http_hdr_ctx;
767 ctx->blk = NULL;
768 smp->ctx.a[0] = ctx;
769 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200770
Christopher Faulet623af932021-01-29 11:22:15 +0100771 if (args[0].type != ARGT_STR)
772 return 0;
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100773 name = ist2(args[0].data.str.area, args[0].data.str.data);
Willy Tarreau79e57332018-10-02 16:01:16 +0200774
Christopher Faulet623af932021-01-29 11:22:15 +0100775 if (args[1].type == ARGT_SINT)
776 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200777
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200778 if (!htx)
779 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200780
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200781 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
782 /* search for header from the beginning */
783 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200784
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200785 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
786 /* no explicit occurrence and single fetch => last header by default */
787 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200788
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200789 if (!occ)
790 /* prepare to report multiple occurrences for ACL fetches */
791 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200792
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200793 smp->data.type = SMP_T_STR;
794 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
795 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
796 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200797 smp->flags &= ~SMP_F_NOT_LAST;
798 return 0;
799}
800
801/* 6. Check on HTTP header count. The number of occurrences is returned.
802 * Accepts exactly 1 argument of type string. It does not stop on commas and
803 * returns full lines instead (useful for User-Agent or Date for example).
804 */
805static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
806{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200807 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
808 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200809 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200810 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200811 struct http_hdr_ctx ctx;
812 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200813 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200814
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200815 if (!htx)
816 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200817
Christopher Faulet623af932021-01-29 11:22:15 +0100818 if (args->type == ARGT_STR) {
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100819 name = ist2(args->data.str.area, args->data.str.data);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200820 } else {
Tim Duesterhus68a088d2021-02-28 16:11:37 +0100821 name = IST_NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200822 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200823
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200824 ctx.blk = NULL;
825 cnt = 0;
826 while (http_find_header(htx, name, &ctx, 1))
827 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200828 smp->data.type = SMP_T_SINT;
829 smp->data.u.sint = cnt;
830 smp->flags = SMP_F_VOL_HDR;
831 return 1;
832}
833
834static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
835{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200836 /* possible keywords: req.hdr_names, res.hdr_names */
837 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200838 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200839 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200840 struct buffer *temp;
841 char del = ',';
842
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200843 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200844
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200845 if (!htx)
846 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200847
Christopher Faulet623af932021-01-29 11:22:15 +0100848 if (args->type == ARGT_STR)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200849 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200850
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200851 temp = get_trash_chunk();
852 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
853 struct htx_blk *blk = htx_get_blk(htx, pos);
854 enum htx_blk_type type = htx_get_blk_type(blk);
855 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200856
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200857 if (type == HTX_BLK_EOH)
858 break;
859 if (type != HTX_BLK_HDR)
860 continue;
861 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200862
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200863 if (temp->data)
864 temp->area[temp->data++] = del;
865 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200866 }
867
868 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200869 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200870 smp->flags = SMP_F_VOL_HDR;
871 return 1;
872}
873
874/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
875 * Accepts an optional argument of type string containing the header field name,
876 * and an optional argument of type signed or unsigned integer to request an
877 * explicit occurrence of the header. Note that in the event of a missing name,
878 * headers are considered from the first one.
879 */
880static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
881{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200882 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
883 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200884 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200885 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200886 struct http_hdr_ctx *ctx = smp->ctx.a[0];
887 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200888 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200889
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200890 if (!ctx) {
891 /* first call */
892 ctx = &static_http_hdr_ctx;
893 ctx->blk = NULL;
894 smp->ctx.a[0] = ctx;
895 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200896
Christopher Faulet623af932021-01-29 11:22:15 +0100897 if (args[0].type != ARGT_STR)
898 return 0;
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100899 name = ist2(args[0].data.str.area, args[0].data.str.data);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200900
Christopher Faulet623af932021-01-29 11:22:15 +0100901 if (args[1].type == ARGT_SINT)
902 occ = args[1].data.sint;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200903
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200904 if (!htx)
905 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200906
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200907 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
908 /* search for header from the beginning */
909 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200910
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200911 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
912 /* no explicit occurrence and single fetch => last header by default */
913 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200914
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200915 if (!occ)
916 /* prepare to report multiple occurrences for ACL fetches */
917 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200918
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200919 smp->data.type = SMP_T_STR;
920 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
921 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
922 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200923
924 smp->flags &= ~SMP_F_NOT_LAST;
925 return 0;
926}
927
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200928/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
929 * the right channel. So instead of duplicating the code, we just change the
930 * keyword and then fallback on smp_fetch_hdr().
931 */
932static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
933{
934 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
935 return smp_fetch_hdr(args, smp, kw, private);
936}
937
Willy Tarreau79e57332018-10-02 16:01:16 +0200938/* 6. Check on HTTP header count. The number of occurrences is returned.
939 * Accepts exactly 1 argument of type string.
940 */
941static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
942{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200943 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
944 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200945 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200946 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200947 struct http_hdr_ctx ctx;
948 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200949 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200950
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200951 if (!htx)
952 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200953
Christopher Faulet623af932021-01-29 11:22:15 +0100954 if (args->type == ARGT_STR) {
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100955 name = ist2(args->data.str.area, args->data.str.data);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200956 } else {
Tim Duesterhus68a088d2021-02-28 16:11:37 +0100957 name = IST_NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200958 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200959
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200960 ctx.blk = NULL;
961 cnt = 0;
962 while (http_find_header(htx, name, &ctx, 0))
963 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200964
965 smp->data.type = SMP_T_SINT;
966 smp->data.u.sint = cnt;
967 smp->flags = SMP_F_VOL_HDR;
968 return 1;
969}
970
971/* Fetch an HTTP header's integer value. The integer value is returned. It
972 * takes a mandatory argument of type string and an optional one of type int
973 * to designate a specific occurrence. It returns an unsigned integer, which
974 * may or may not be appropriate for everything.
975 */
976static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
977{
978 int ret = smp_fetch_hdr(args, smp, kw, private);
979
980 if (ret > 0) {
981 smp->data.type = SMP_T_SINT;
982 smp->data.u.sint = strl2ic(smp->data.u.str.area,
983 smp->data.u.str.data);
984 }
985
986 return ret;
987}
988
989/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
990 * and an optional one of type int to designate a specific occurrence.
991 * It returns an IPv4 or IPv6 address.
992 */
993static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
994{
995 int ret;
Tim Duesterhus5cd00872020-06-26 15:44:48 +0200996 struct buffer *temp = get_trash_chunk();
Willy Tarreau79e57332018-10-02 16:01:16 +0200997
998 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Tim Duesterhus5cd00872020-06-26 15:44:48 +0200999 if (smp->data.u.str.data < temp->size - 1) {
1000 memcpy(temp->area, smp->data.u.str.area,
1001 smp->data.u.str.data);
1002 temp->area[smp->data.u.str.data] = '\0';
1003 if (url2ipv4((char *) temp->area, &smp->data.u.ipv4)) {
1004 smp->data.type = SMP_T_IPV4;
1005 break;
1006 } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1007 smp->data.type = SMP_T_IPV6;
1008 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001009 }
1010 }
1011
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001012 /* if the header doesn't match an IP address, fetch next one */
1013 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001014 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001015 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001016 return ret;
1017}
Willy Tarreau79e57332018-10-02 16:01:16 +02001018
Christopher Faulete720c322020-09-02 17:25:18 +02001019/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at the
1020 * first '/' after the possible hostname. It ends before the possible '?' except
1021 * for 'pathq' keyword.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001022 */
1023static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1024{
1025 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001026 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001027 struct htx_sl *sl;
1028 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001029
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001030 if (!htx)
1031 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001032
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001033 sl = http_get_stline(htx);
Christopher Faulete720c322020-09-02 17:25:18 +02001034 path = http_get_path(htx_sl_req_uri(sl));
1035
Yves Lafonb4d37082021-02-11 11:01:28 +01001036 if (kw[4] == 'q' && (kw[0] == 'p' || kw[0] == 'b')) // pathq or baseq
Christopher Faulete720c322020-09-02 17:25:18 +02001037 path = http_get_path(htx_sl_req_uri(sl));
1038 else
1039 path = iststop(http_get_path(htx_sl_req_uri(sl)), '?');
1040
Tim Duesterhused526372020-03-05 17:56:33 +01001041 if (!isttest(path))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001042 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001043
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001044 /* OK, we got the '/' ! */
1045 smp->data.type = SMP_T_STR;
1046 smp->data.u.str.area = path.ptr;
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001047 smp->data.u.str.data = path.len;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001048 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001049 return 1;
1050}
1051
1052/* This produces a concatenation of the first occurrence of the Host header
1053 * followed by the path component if it begins with a slash ('/'). This means
1054 * that '*' will not be added, resulting in exactly the first Host entry.
1055 * If no Host header is found, then the path is returned as-is. The returned
1056 * value is stored in the trash so it does not need to be marked constant.
1057 * The returned sample is of type string.
1058 */
1059static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1060{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001061 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001062 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001063 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001064 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001065 struct http_hdr_ctx ctx;
1066 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001067
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001068 if (!htx)
1069 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001070
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001071 ctx.blk = NULL;
1072 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1073 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001074
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001075 /* OK we have the header value in ctx.value */
1076 temp = get_trash_chunk();
1077 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001078
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001079 /* now retrieve the path */
1080 sl = http_get_stline(htx);
1081 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001082 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001083 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001084
Yves Lafonb4d37082021-02-11 11:01:28 +01001085 if (kw[4] == 'q' && kw[0] == 'b') { // baseq
1086 len = path.len;
1087 } else {
1088 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1089 ;
1090 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001091
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001092 if (len && *(path.ptr) == '/')
1093 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001094 }
1095
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001096 smp->data.type = SMP_T_STR;
1097 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001098 smp->flags = SMP_F_VOL_1ST;
1099 return 1;
1100}
1101
1102/* This produces a 32-bit hash of the concatenation of the first occurrence of
1103 * the Host header followed by the path component if it begins with a slash ('/').
1104 * This means that '*' will not be added, resulting in exactly the first Host
1105 * entry. If no Host header is found, then the path is used. The resulting value
1106 * is hashed using the path hash followed by a full avalanche hash and provides a
1107 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1108 * high-traffic sites without having to store whole paths.
1109 */
1110static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1111{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001112 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001113 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001114 struct htx_sl *sl;
1115 struct http_hdr_ctx ctx;
1116 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001117 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001118
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001119 if (!htx)
1120 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001121
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001122 ctx.blk = NULL;
1123 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1124 /* OK we have the header value in ctx.value */
1125 while (ctx.value.len--)
1126 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001127 }
1128
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001129 /* now retrieve the path */
1130 sl = http_get_stline(htx);
1131 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001132 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001133 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001134
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001135 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1136 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001137
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001138 if (len && *(path.ptr) == '/') {
1139 while (len--)
1140 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001141 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001142 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001143
Willy Tarreau79e57332018-10-02 16:01:16 +02001144 hash = full_hash(hash);
1145
1146 smp->data.type = SMP_T_SINT;
1147 smp->data.u.sint = hash;
1148 smp->flags = SMP_F_VOL_1ST;
1149 return 1;
1150}
1151
1152/* This concatenates the source address with the 32-bit hash of the Host and
1153 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1154 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1155 * on the source address length. The path hash is stored before the address so
1156 * that in environments where IPv6 is insignificant, truncating the output to
1157 * 8 bytes would still work.
1158 */
1159static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1160{
1161 struct buffer *temp;
1162 struct connection *cli_conn = objt_conn(smp->sess->origin);
1163
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001164 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001165 return 0;
1166
1167 if (!smp_fetch_base32(args, smp, kw, private))
1168 return 0;
1169
1170 temp = get_trash_chunk();
1171 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1172 temp->data += sizeof(unsigned int);
1173
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001174 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001175 case AF_INET:
1176 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001177 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001178 4);
1179 temp->data += 4;
1180 break;
1181 case AF_INET6:
1182 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001183 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001184 16);
1185 temp->data += 16;
1186 break;
1187 default:
1188 return 0;
1189 }
1190
1191 smp->data.u.str = *temp;
1192 smp->data.type = SMP_T_BIN;
1193 return 1;
1194}
1195
1196/* Extracts the query string, which comes after the question mark '?'. If no
1197 * question mark is found, nothing is returned. Otherwise it returns a sample
1198 * of type string carrying the whole query string.
1199 */
1200static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1201{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001202 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001203 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001204 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001205 char *ptr, *end;
1206
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001207 if (!htx)
1208 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001209
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001210 sl = http_get_stline(htx);
1211 ptr = HTX_SL_REQ_UPTR(sl);
1212 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001213
1214 /* look up the '?' */
1215 do {
1216 if (ptr == end)
1217 return 0;
1218 } while (*ptr++ != '?');
1219
1220 smp->data.type = SMP_T_STR;
1221 smp->data.u.str.area = ptr;
1222 smp->data.u.str.data = end - ptr;
1223 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1224 return 1;
1225}
1226
1227static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1228{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001229 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001230 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001231
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001232 if (!htx)
1233 return 0;
1234 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001235 smp->data.u.sint = 1;
1236 return 1;
1237}
1238
1239/* return a valid test if the current request is the first one on the connection */
1240static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1241{
Willy Tarreau79512b62020-04-29 11:52:13 +02001242 if (!smp->strm)
1243 return 0;
1244
Willy Tarreau79e57332018-10-02 16:01:16 +02001245 smp->data.type = SMP_T_BOOL;
1246 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1247 return 1;
1248}
1249
Christopher Fauleta4063562019-08-02 11:51:37 +02001250/* Fetch the authentication method if there is an Authorization header. It
1251 * relies on get_http_auth()
1252 */
1253static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1254{
1255 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001256 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001257 struct http_txn *txn;
1258
1259 if (!htx)
1260 return 0;
1261
1262 txn = smp->strm->txn;
1263 if (!get_http_auth(smp, htx))
1264 return 0;
1265
1266 switch (txn->auth.method) {
1267 case HTTP_AUTH_BASIC:
1268 smp->data.u.str.area = "Basic";
1269 smp->data.u.str.data = 5;
1270 break;
1271 case HTTP_AUTH_DIGEST:
1272 /* Unexpected because not supported */
1273 smp->data.u.str.area = "Digest";
1274 smp->data.u.str.data = 6;
1275 break;
1276 default:
1277 return 0;
1278 }
1279
1280 smp->data.type = SMP_T_STR;
1281 smp->flags = SMP_F_CONST;
1282 return 1;
1283}
1284
1285/* Fetch the user supplied if there is an Authorization header. It relies on
1286 * get_http_auth()
1287 */
1288static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1289{
1290 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001291 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001292 struct http_txn *txn;
1293
1294 if (!htx)
1295 return 0;
1296
1297 txn = smp->strm->txn;
1298 if (!get_http_auth(smp, htx))
1299 return 0;
1300
1301 smp->data.type = SMP_T_STR;
1302 smp->data.u.str.area = txn->auth.user;
1303 smp->data.u.str.data = strlen(txn->auth.user);
1304 smp->flags = SMP_F_CONST;
1305 return 1;
1306}
1307
1308/* Fetch the password supplied if there is an Authorization header. It relies on
1309 * get_http_auth()
1310 */
1311static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1312{
1313 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001314 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001315 struct http_txn *txn;
1316
1317 if (!htx)
1318 return 0;
1319
1320 txn = smp->strm->txn;
1321 if (!get_http_auth(smp, htx))
1322 return 0;
1323
1324 smp->data.type = SMP_T_STR;
1325 smp->data.u.str.area = txn->auth.pass;
1326 smp->data.u.str.data = strlen(txn->auth.pass);
1327 smp->flags = SMP_F_CONST;
1328 return 1;
1329}
1330
Willy Tarreau79e57332018-10-02 16:01:16 +02001331/* Accepts exactly 1 argument of type userlist */
1332static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1333{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001334 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001335 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001336
Christopher Faulet623af932021-01-29 11:22:15 +01001337 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001338 return 0;
1339
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001340 if (!htx)
1341 return 0;
1342 if (!get_http_auth(smp, htx))
1343 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001344
1345 smp->data.type = SMP_T_BOOL;
1346 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001347 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001348 return 1;
1349}
1350
1351/* Accepts exactly 1 argument of type userlist */
1352static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1353{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001354 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001355 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001356
Christopher Faulet623af932021-01-29 11:22:15 +01001357 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001358 return 0;
1359
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001360 if (!htx)
1361 return 0;
1362 if (!get_http_auth(smp, htx))
1363 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001364
Willy Tarreau79e57332018-10-02 16:01:16 +02001365 /* if the user does not belong to the userlist or has a wrong password,
1366 * report that it unconditionally does not match. Otherwise we return
1367 * a string containing the username.
1368 */
1369 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1370 smp->strm->txn->auth.pass))
1371 return 0;
1372
1373 /* pat_match_auth() will need the user list */
1374 smp->ctx.a[0] = args->data.usr;
1375
1376 smp->data.type = SMP_T_STR;
1377 smp->flags = SMP_F_CONST;
1378 smp->data.u.str.area = smp->strm->txn->auth.user;
1379 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1380
1381 return 1;
1382}
1383
1384/* Fetch a captured HTTP request header. The index is the position of
1385 * the "capture" option in the configuration file
1386 */
1387static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1388{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001389 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001390 int idx;
1391
Christopher Faulet623af932021-01-29 11:22:15 +01001392 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001393 return 0;
1394
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001395 if (!smp->strm)
1396 return 0;
1397
1398 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001399 idx = args->data.sint;
1400
1401 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1402 return 0;
1403
1404 smp->data.type = SMP_T_STR;
1405 smp->flags |= SMP_F_CONST;
1406 smp->data.u.str.area = smp->strm->req_cap[idx];
1407 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1408
1409 return 1;
1410}
1411
1412/* Fetch a captured HTTP response header. The index is the position of
1413 * the "capture" option in the configuration file
1414 */
1415static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1416{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001417 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001418 int idx;
1419
Christopher Faulet623af932021-01-29 11:22:15 +01001420 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001421 return 0;
1422
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001423 if (!smp->strm)
1424 return 0;
1425
1426 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001427 idx = args->data.sint;
1428
1429 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1430 return 0;
1431
1432 smp->data.type = SMP_T_STR;
1433 smp->flags |= SMP_F_CONST;
1434 smp->data.u.str.area = smp->strm->res_cap[idx];
1435 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1436
1437 return 1;
1438}
1439
1440/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1441static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1442{
1443 struct buffer *temp;
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001444 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001445 char *ptr;
1446
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001447 if (!smp->strm)
1448 return 0;
1449
1450 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001451 if (!txn || !txn->uri)
1452 return 0;
1453
1454 ptr = txn->uri;
1455
1456 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1457 ptr++;
1458
1459 temp = get_trash_chunk();
1460 temp->area = txn->uri;
1461 temp->data = ptr - txn->uri;
1462 smp->data.u.str = *temp;
1463 smp->data.type = SMP_T_STR;
1464 smp->flags = SMP_F_CONST;
1465
1466 return 1;
1467
1468}
1469
1470/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1471static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1472{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001473 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001474 struct ist path;
1475 const char *ptr;
1476
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001477 if (!smp->strm)
1478 return 0;
1479
1480 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001481 if (!txn || !txn->uri)
1482 return 0;
1483
1484 ptr = txn->uri;
1485
1486 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1487 ptr++;
1488
1489 if (!*ptr)
1490 return 0;
1491
Christopher Faulet78337bb2018-11-15 14:35:18 +01001492 /* skip the first space and find space after URI */
1493 path = ist2(++ptr, 0);
1494 while (*ptr != ' ' && *ptr != '\0')
1495 ptr++;
1496 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001497
Christopher Faulet78337bb2018-11-15 14:35:18 +01001498 path = http_get_path(path);
Tim Duesterhused526372020-03-05 17:56:33 +01001499 if (!isttest(path))
Willy Tarreau79e57332018-10-02 16:01:16 +02001500 return 0;
1501
1502 smp->data.u.str.area = path.ptr;
1503 smp->data.u.str.data = path.len;
1504 smp->data.type = SMP_T_STR;
1505 smp->flags = SMP_F_CONST;
1506
1507 return 1;
1508}
1509
1510/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1511 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1512 */
1513static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1514{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001515 struct http_txn *txn;
1516
1517 if (!smp->strm)
1518 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001519
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001520 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001521 if (!txn || txn->req.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001522 return 0;
1523
1524 if (txn->req.flags & HTTP_MSGF_VER_11)
1525 smp->data.u.str.area = "HTTP/1.1";
1526 else
1527 smp->data.u.str.area = "HTTP/1.0";
1528
1529 smp->data.u.str.data = 8;
1530 smp->data.type = SMP_T_STR;
1531 smp->flags = SMP_F_CONST;
1532 return 1;
1533
1534}
1535
1536/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1537 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1538 */
1539static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1540{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001541 struct http_txn *txn;
1542
1543 if (!smp->strm)
1544 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001545
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001546 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001547 if (!txn || txn->rsp.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001548 return 0;
1549
1550 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1551 smp->data.u.str.area = "HTTP/1.1";
1552 else
1553 smp->data.u.str.area = "HTTP/1.0";
1554
1555 smp->data.u.str.data = 8;
1556 smp->data.type = SMP_T_STR;
1557 smp->flags = SMP_F_CONST;
1558 return 1;
1559
1560}
1561
1562/* Iterate over all cookies present in a message. The context is stored in
1563 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1564 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1565 * the direction, multiple cookies may be parsed on the same line or not.
Maciej Zdebdea7c202020-11-13 09:38:06 +00001566 * If provided, the searched cookie name is in args, in args->data.str. If
1567 * the input options indicate that no iterating is desired, then only last
1568 * value is fetched if any. If no cookie name is provided, the first cookie
1569 * value found is fetched. The returned sample is of type CSTR. Can be used
1570 * to parse cookies in other files.
Willy Tarreau79e57332018-10-02 16:01:16 +02001571 */
1572static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1573{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001574 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1575 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001576 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001577 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001578 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1579 struct ist hdr;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001580 char *cook = NULL;
1581 size_t cook_l = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001582 int found = 0;
1583
Christopher Faulet623af932021-01-29 11:22:15 +01001584 if (args->type == ARGT_STR) {
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001585 cook = args->data.str.area;
1586 cook_l = args->data.str.data;
1587 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001588
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001589 if (!ctx) {
1590 /* first call */
1591 ctx = &static_http_hdr_ctx;
1592 ctx->blk = NULL;
1593 smp->ctx.a[2] = ctx;
1594 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001595
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001596 if (!htx)
1597 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001598
Christopher Faulet16032ab2020-04-30 11:30:00 +02001599 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001600
Maciej Zdebdea7c202020-11-13 09:38:06 +00001601 /* OK so basically here, either we want only one value or we want to
1602 * iterate over all of them and we fetch the next one. In this last case
1603 * SMP_OPT_ITERATE option is set.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001604 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001605
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001606 if (!(smp->flags & SMP_F_NOT_LAST)) {
1607 /* search for the header from the beginning, we must first initialize
1608 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001609 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001610 smp->ctx.a[0] = NULL;
1611 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001612 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001613
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001614 smp->flags |= SMP_F_VOL_HDR;
1615 while (1) {
1616 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1617 if (!smp->ctx.a[0]) {
1618 if (!http_find_header(htx, hdr, ctx, 0))
1619 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001620
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001621 if (ctx->value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001622 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001623
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001624 smp->ctx.a[0] = ctx->value.ptr;
1625 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001626 }
1627
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001628 smp->data.type = SMP_T_STR;
1629 smp->flags |= SMP_F_CONST;
1630 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001631 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001632 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1633 &smp->data.u.str.area,
1634 &smp->data.u.str.data);
1635 if (smp->ctx.a[0]) {
1636 found = 1;
Maciej Zdebdea7c202020-11-13 09:38:06 +00001637 if (smp->opt & SMP_OPT_ITERATE) {
1638 /* iterate on cookie value */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001639 smp->flags |= SMP_F_NOT_LAST;
1640 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001641 }
Maciej Zdebdea7c202020-11-13 09:38:06 +00001642 if (args->data.str.data == 0) {
1643 /* No cookie name, first occurrence returned */
1644 break;
1645 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001646 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001647 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001648 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001649
Willy Tarreau79e57332018-10-02 16:01:16 +02001650 /* all cookie headers and values were scanned. If we're looking for the
1651 * last occurrence, we may return it now.
1652 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001653 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001654 smp->flags &= ~SMP_F_NOT_LAST;
1655 return found;
1656}
1657
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001658/* Same than smp_fetch_cookie() but only relies on the sample direction to
1659 * choose the right channel. So instead of duplicating the code, we just change
1660 * the keyword and then fallback on smp_fetch_cookie().
1661 */
1662static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1663{
1664 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1665 return smp_fetch_cookie(args, smp, kw, private);
1666}
1667
Willy Tarreau79e57332018-10-02 16:01:16 +02001668/* Iterate over all cookies present in a request to count how many occurrences
1669 * match the name in args and args->data.str.len. If <multi> is non-null, then
1670 * multiple cookies may be parsed on the same line. The returned sample is of
1671 * type UINT. Accepts exactly 1 argument of type string.
1672 */
1673static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1674{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001675 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1676 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001677 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001678 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001679 struct http_hdr_ctx ctx;
1680 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001681 char *val_beg, *val_end;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001682 char *cook = NULL;
1683 size_t cook_l = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001684 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001685
Christopher Faulet623af932021-01-29 11:22:15 +01001686 if (args->type == ARGT_STR){
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001687 cook = args->data.str.area;
1688 cook_l = args->data.str.data;
1689 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001690
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001691 if (!htx)
1692 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001693
Christopher Faulet16032ab2020-04-30 11:30:00 +02001694 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001695
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001696 val_end = val_beg = NULL;
1697 ctx.blk = NULL;
1698 cnt = 0;
1699 while (1) {
1700 /* Note: val_beg == NULL every time we need to fetch a new header */
1701 if (!val_beg) {
1702 if (!http_find_header(htx, hdr, &ctx, 0))
1703 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001704
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001705 if (ctx.value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001706 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001707
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001708 val_beg = ctx.value.ptr;
1709 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001710 }
1711
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001712 smp->data.type = SMP_T_STR;
1713 smp->flags |= SMP_F_CONST;
1714 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001715 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001716 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1717 &smp->data.u.str.area,
1718 &smp->data.u.str.data))) {
1719 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001720 }
1721 }
1722
1723 smp->data.type = SMP_T_SINT;
1724 smp->data.u.sint = cnt;
1725 smp->flags |= SMP_F_VOL_HDR;
1726 return 1;
1727}
1728
1729/* Fetch an cookie's integer value. The integer value is returned. It
1730 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1731 */
1732static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1733{
1734 int ret = smp_fetch_cookie(args, smp, kw, private);
1735
1736 if (ret > 0) {
1737 smp->data.type = SMP_T_SINT;
1738 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1739 smp->data.u.str.data);
1740 }
1741
1742 return ret;
1743}
1744
1745/************************************************************************/
1746/* The code below is dedicated to sample fetches */
1747/************************************************************************/
1748
1749/* This scans a URL-encoded query string. It takes an optionally wrapping
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001750 * string whose first contiguous chunk has its beginning in ctx->a[0] and end
Willy Tarreau79e57332018-10-02 16:01:16 +02001751 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1752 * pointers are updated for next iteration before leaving.
1753 */
1754static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1755{
1756 const char *vstart, *vend;
1757 struct buffer *temp;
1758 const char **chunks = (const char **)smp->ctx.a;
1759
1760 if (!http_find_next_url_param(chunks, name, name_len,
1761 &vstart, &vend, delim))
1762 return 0;
1763
1764 /* Create sample. If the value is contiguous, return the pointer as CONST,
1765 * if the value is wrapped, copy-it in a buffer.
1766 */
1767 smp->data.type = SMP_T_STR;
1768 if (chunks[2] &&
1769 vstart >= chunks[0] && vstart <= chunks[1] &&
1770 vend >= chunks[2] && vend <= chunks[3]) {
1771 /* Wrapped case. */
1772 temp = get_trash_chunk();
1773 memcpy(temp->area, vstart, chunks[1] - vstart);
1774 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1775 vend - chunks[2]);
1776 smp->data.u.str.area = temp->area;
1777 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1778 } else {
1779 /* Contiguous case. */
1780 smp->data.u.str.area = (char *)vstart;
1781 smp->data.u.str.data = vend - vstart;
1782 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1783 }
1784
1785 /* Update context, check wrapping. */
1786 chunks[0] = vend;
1787 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1788 chunks[1] = chunks[3];
1789 chunks[2] = NULL;
1790 }
1791
1792 if (chunks[0] < chunks[1])
1793 smp->flags |= SMP_F_NOT_LAST;
1794
1795 return 1;
1796}
1797
1798/* This function iterates over each parameter of the query string. It uses
1799 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1800 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1801 * An optional parameter name is passed in args[0], otherwise any parameter is
1802 * considered. It supports an optional delimiter argument for the beginning of
1803 * the string in args[1], which defaults to "?".
1804 */
1805static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1806{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001807 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001808 char delim = '?';
1809 const char *name;
1810 int name_len;
1811
Christopher Faulet623af932021-01-29 11:22:15 +01001812 if ((args[0].type && args[0].type != ARGT_STR) ||
Willy Tarreau79e57332018-10-02 16:01:16 +02001813 (args[1].type && args[1].type != ARGT_STR))
1814 return 0;
1815
1816 name = "";
1817 name_len = 0;
1818 if (args->type == ARGT_STR) {
1819 name = args->data.str.area;
1820 name_len = args->data.str.data;
1821 }
1822
1823 if (args[1].type)
1824 delim = *args[1].data.str.area;
1825
1826 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001827 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001828 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001829
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001830 if (!htx)
1831 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001832
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001833 sl = http_get_stline(htx);
1834 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1835 if (!smp->ctx.a[0])
1836 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001837
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001838 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001839
1840 /* Assume that the context is filled with NULL pointer
1841 * before the first call.
1842 * smp->ctx.a[2] = NULL;
1843 * smp->ctx.a[3] = NULL;
1844 */
1845 }
1846
1847 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1848}
1849
1850/* This function iterates over each parameter of the body. This requires
1851 * that the body has been waited for using http-buffer-request. It uses
1852 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001853 * contiguous part of the body, and optionally ctx->a[2..3] to reference the
Willy Tarreau79e57332018-10-02 16:01:16 +02001854 * optional second part if the body wraps at the end of the buffer. An optional
1855 * parameter name is passed in args[0], otherwise any parameter is considered.
1856 */
1857static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1858{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001859 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001860 const char *name;
1861 int name_len;
1862
Christopher Faulet623af932021-01-29 11:22:15 +01001863 if (args[0].type && args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001864 return 0;
1865
1866 name = "";
1867 name_len = 0;
1868 if (args[0].type == ARGT_STR) {
1869 name = args[0].data.str.area;
1870 name_len = args[0].data.str.data;
1871 }
1872
1873 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulete596d182020-05-05 17:46:34 +02001874 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001875 struct buffer *temp;
1876 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001877
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001878 if (!htx)
1879 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001880
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001881 temp = get_trash_chunk();
1882 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1883 struct htx_blk *blk = htx_get_blk(htx, pos);
1884 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001885
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001886 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001887 break;
1888 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001889 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001890 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001891 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001892 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001893
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001894 smp->ctx.a[0] = temp->area;
1895 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001896
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001897 /* Assume that the context is filled with NULL pointer
1898 * before the first call.
1899 * smp->ctx.a[2] = NULL;
1900 * smp->ctx.a[3] = NULL;
1901 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001902
Willy Tarreau79e57332018-10-02 16:01:16 +02001903 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001904
Willy Tarreau79e57332018-10-02 16:01:16 +02001905 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1906}
1907
1908/* Return the signed integer value for the specified url parameter (see url_param
1909 * above).
1910 */
1911static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1912{
1913 int ret = smp_fetch_url_param(args, smp, kw, private);
1914
1915 if (ret > 0) {
1916 smp->data.type = SMP_T_SINT;
1917 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1918 smp->data.u.str.data);
1919 }
1920
1921 return ret;
1922}
1923
1924/* This produces a 32-bit hash of the concatenation of the first occurrence of
1925 * the Host header followed by the path component if it begins with a slash ('/').
1926 * This means that '*' will not be added, resulting in exactly the first Host
1927 * entry. If no Host header is found, then the path is used. The resulting value
1928 * is hashed using the url hash followed by a full avalanche hash and provides a
1929 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1930 * high-traffic sites without having to store whole paths.
1931 * this differs from the base32 functions in that it includes the url parameters
1932 * as well as the path
1933 */
1934static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1935{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001936 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001937 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001938 struct http_hdr_ctx ctx;
1939 struct htx_sl *sl;
1940 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001941 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001942
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001943 if (!htx)
1944 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001945
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001946 ctx.blk = NULL;
1947 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1948 /* OK we have the header value in ctx.value */
1949 while (ctx.value.len--)
1950 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001951 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001952
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001953 /* now retrieve the path */
1954 sl = http_get_stline(htx);
1955 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001956 if (path.len && *(path.ptr) == '/') {
1957 while (path.len--)
1958 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001959 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001960
Willy Tarreau79e57332018-10-02 16:01:16 +02001961 hash = full_hash(hash);
1962
1963 smp->data.type = SMP_T_SINT;
1964 smp->data.u.sint = hash;
1965 smp->flags = SMP_F_VOL_1ST;
1966 return 1;
1967}
1968
1969/* This concatenates the source address with the 32-bit hash of the Host and
1970 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1971 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1972 * on the source address length. The URL hash is stored before the address so
1973 * that in environments where IPv6 is insignificant, truncating the output to
1974 * 8 bytes would still work.
1975 */
1976static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1977{
1978 struct buffer *temp;
1979 struct connection *cli_conn = objt_conn(smp->sess->origin);
1980
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001981 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001982 return 0;
1983
1984 if (!smp_fetch_url32(args, smp, kw, private))
1985 return 0;
1986
1987 temp = get_trash_chunk();
1988 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1989 temp->data += sizeof(unsigned int);
1990
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001991 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001992 case AF_INET:
1993 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001994 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001995 4);
1996 temp->data += 4;
1997 break;
1998 case AF_INET6:
1999 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002000 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002001 16);
2002 temp->data += 16;
2003 break;
2004 default:
2005 return 0;
2006 }
2007
2008 smp->data.u.str = *temp;
2009 smp->data.type = SMP_T_BIN;
2010 return 1;
2011}
2012
2013/************************************************************************/
2014/* Other utility functions */
2015/************************************************************************/
2016
2017/* This function is used to validate the arguments passed to any "hdr" fetch
2018 * keyword. These keywords support an optional positive or negative occurrence
2019 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2020 * is assumed that the types are already the correct ones. Returns 0 on error,
2021 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2022 * error message in case of error, that the caller is responsible for freeing.
2023 * The initial location must either be freeable or NULL.
2024 * Note: this function's pointer is checked from Lua.
2025 */
2026int val_hdr(struct arg *arg, char **err_msg)
2027{
2028 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2029 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2030 return 0;
2031 }
2032 return 1;
2033}
2034
2035/************************************************************************/
2036/* All supported sample fetch keywords must be declared here. */
2037/************************************************************************/
2038
2039/* Note: must not be declared <const> as its list will be overwritten */
2040static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2041 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2042 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2043 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Yves Lafonb4d37082021-02-11 11:01:28 +01002044 { "baseq", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002045
2046 /* capture are allocated and are permanent in the stream */
2047 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2048
2049 /* retrieve these captures from the HTTP logs */
2050 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2051 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2052 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2053
2054 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2055 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2056
2057 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2058 * are only here to match the ACL's name, are request-only and are used
2059 * for ACL compatibility only.
2060 */
2061 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002062 { "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 +02002063 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2064 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2065
2066 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2067 * only here to match the ACL's name, are request-only and are used for
2068 * ACL compatibility only.
2069 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002070 { "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 +02002071 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2072 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2073 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2074
Christopher Fauleta4063562019-08-02 11:51:37 +02002075 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2076 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2077 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002078 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2079 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2080 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2081 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2082 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Faulete720c322020-09-02 17:25:18 +02002083 { "pathq", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002084 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2085
2086 /* HTTP protocol on the request path */
2087 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2088 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2089
2090 /* HTTP version on the request path */
2091 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2092 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2093
2094 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2095 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2096 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2097 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2098
2099 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2100 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2101
2102 /* HTTP version on the response path */
2103 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2104 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2105
Christopher Faulete596d182020-05-05 17:46:34 +02002106 { "res.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2107 { "res.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2108 { "res.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2109
2110 { "res.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2111 { "res.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2112
Willy Tarreau79e57332018-10-02 16:01:16 +02002113 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2114 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2115 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2116 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2117
2118 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2119 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2120 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2121 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2122 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2123 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2124 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2125
2126 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2127 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2128 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2129 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2130
2131 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2132 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2133 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2134 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2135 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2136 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2137 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2138
2139 /* scook is valid only on the response and is used for ACL compatibility */
2140 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2141 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2142 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2143 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2144
2145 /* shdr is valid only on the response and is used for ACL compatibility */
2146 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2147 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2148 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2149 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2150
2151 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2152 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2153 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2154 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2155 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2156 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2157 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2158 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2159 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2160 { "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 +02002161
Willy Tarreau79e57332018-10-02 16:01:16 +02002162 { /* END */ },
2163}};
2164
Willy Tarreau0108d902018-11-25 19:14:37 +01002165INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002166
2167/*
2168 * Local variables:
2169 * c-indent-level: 8
2170 * c-basic-offset: 8
2171 * End:
2172 */