blob: 6c569a75bb877f2716c39545602559301ca8428d [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.
Willy Tarreau7b0e00d2021-03-25 14:12:29 +0100991 * It returns an IPv4 or IPv6 address. Addresses surrounded by invalid chars
992 * are rejected. However IPv4 addresses may be followed with a colon and a
993 * valid port number.
Willy Tarreau79e57332018-10-02 16:01:16 +0200994 */
995static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
996{
Tim Duesterhus5cd00872020-06-26 15:44:48 +0200997 struct buffer *temp = get_trash_chunk();
Willy Tarreau7b0e00d2021-03-25 14:12:29 +0100998 int ret, len;
999 int port;
Willy Tarreau79e57332018-10-02 16:01:16 +02001000
1001 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001002 if (smp->data.u.str.data < temp->size - 1) {
1003 memcpy(temp->area, smp->data.u.str.area,
1004 smp->data.u.str.data);
1005 temp->area[smp->data.u.str.data] = '\0';
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001006 len = url2ipv4((char *) temp->area, &smp->data.u.ipv4);
Willy Tarreau645dc082021-03-31 11:41:36 +02001007 if (len > 0 && len == smp->data.u.str.data) {
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001008 /* plain IPv4 address */
1009 smp->data.type = SMP_T_IPV4;
1010 break;
1011 } else if (len > 0 && temp->area[len] == ':' &&
1012 strl2irc(temp->area + len + 1, smp->data.u.str.data - len - 1, &port) == 0 &&
1013 port >= 0 && port <= 65535) {
1014 /* IPv4 address suffixed with ':' followed by a valid port number */
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001015 smp->data.type = SMP_T_IPV4;
1016 break;
1017 } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1018 smp->data.type = SMP_T_IPV6;
1019 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001020 }
1021 }
1022
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001023 /* if the header doesn't match an IP address, fetch next one */
1024 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001025 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001026 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001027 return ret;
1028}
Willy Tarreau79e57332018-10-02 16:01:16 +02001029
Christopher Faulete720c322020-09-02 17:25:18 +02001030/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at the
1031 * first '/' after the possible hostname. It ends before the possible '?' except
1032 * for 'pathq' keyword.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001033 */
1034static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1035{
1036 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001037 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001038 struct htx_sl *sl;
1039 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001040
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001041 if (!htx)
1042 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001043
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001044 sl = http_get_stline(htx);
Christopher Faulete720c322020-09-02 17:25:18 +02001045 path = http_get_path(htx_sl_req_uri(sl));
1046
Yves Lafonb4d37082021-02-11 11:01:28 +01001047 if (kw[4] == 'q' && (kw[0] == 'p' || kw[0] == 'b')) // pathq or baseq
Christopher Faulete720c322020-09-02 17:25:18 +02001048 path = http_get_path(htx_sl_req_uri(sl));
1049 else
1050 path = iststop(http_get_path(htx_sl_req_uri(sl)), '?');
1051
Tim Duesterhused526372020-03-05 17:56:33 +01001052 if (!isttest(path))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001053 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001054
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001055 /* OK, we got the '/' ! */
1056 smp->data.type = SMP_T_STR;
1057 smp->data.u.str.area = path.ptr;
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001058 smp->data.u.str.data = path.len;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001059 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001060 return 1;
1061}
1062
1063/* This produces a concatenation of the first occurrence of the Host header
1064 * followed by the path component if it begins with a slash ('/'). This means
1065 * that '*' will not be added, resulting in exactly the first Host entry.
1066 * If no Host header is found, then the path is returned as-is. The returned
1067 * value is stored in the trash so it does not need to be marked constant.
1068 * The returned sample is of type string.
1069 */
1070static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1071{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001072 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001073 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001074 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001075 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001076 struct http_hdr_ctx ctx;
1077 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001078
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001079 if (!htx)
1080 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001081
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001082 ctx.blk = NULL;
1083 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1084 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001085
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001086 /* OK we have the header value in ctx.value */
1087 temp = get_trash_chunk();
1088 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001089
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001090 /* now retrieve the path */
1091 sl = http_get_stline(htx);
1092 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001093 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001094 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001095
Yves Lafonb4d37082021-02-11 11:01:28 +01001096 if (kw[4] == 'q' && kw[0] == 'b') { // baseq
1097 len = path.len;
1098 } else {
1099 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1100 ;
1101 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001102
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001103 if (len && *(path.ptr) == '/')
1104 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001105 }
1106
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001107 smp->data.type = SMP_T_STR;
1108 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001109 smp->flags = SMP_F_VOL_1ST;
1110 return 1;
1111}
1112
1113/* This produces a 32-bit hash of the concatenation of the first occurrence of
1114 * the Host header followed by the path component if it begins with a slash ('/').
1115 * This means that '*' will not be added, resulting in exactly the first Host
1116 * entry. If no Host header is found, then the path is used. The resulting value
1117 * is hashed using the path hash followed by a full avalanche hash and provides a
1118 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1119 * high-traffic sites without having to store whole paths.
1120 */
1121static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1122{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001123 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001124 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001125 struct htx_sl *sl;
1126 struct http_hdr_ctx ctx;
1127 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001128 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001129
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001130 if (!htx)
1131 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001132
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001133 ctx.blk = NULL;
1134 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1135 /* OK we have the header value in ctx.value */
1136 while (ctx.value.len--)
1137 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001138 }
1139
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001140 /* now retrieve the path */
1141 sl = http_get_stline(htx);
1142 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001143 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001144 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001145
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001146 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1147 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001148
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001149 if (len && *(path.ptr) == '/') {
1150 while (len--)
1151 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001152 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001153 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001154
Willy Tarreau79e57332018-10-02 16:01:16 +02001155 hash = full_hash(hash);
1156
1157 smp->data.type = SMP_T_SINT;
1158 smp->data.u.sint = hash;
1159 smp->flags = SMP_F_VOL_1ST;
1160 return 1;
1161}
1162
1163/* This concatenates the source address with the 32-bit hash of the Host and
1164 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1165 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1166 * on the source address length. The path hash is stored before the address so
1167 * that in environments where IPv6 is insignificant, truncating the output to
1168 * 8 bytes would still work.
1169 */
1170static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1171{
1172 struct buffer *temp;
1173 struct connection *cli_conn = objt_conn(smp->sess->origin);
1174
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001175 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001176 return 0;
1177
1178 if (!smp_fetch_base32(args, smp, kw, private))
1179 return 0;
1180
1181 temp = get_trash_chunk();
1182 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1183 temp->data += sizeof(unsigned int);
1184
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001185 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001186 case AF_INET:
1187 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001188 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001189 4);
1190 temp->data += 4;
1191 break;
1192 case AF_INET6:
1193 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001194 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001195 16);
1196 temp->data += 16;
1197 break;
1198 default:
1199 return 0;
1200 }
1201
1202 smp->data.u.str = *temp;
1203 smp->data.type = SMP_T_BIN;
1204 return 1;
1205}
1206
1207/* Extracts the query string, which comes after the question mark '?'. If no
1208 * question mark is found, nothing is returned. Otherwise it returns a sample
1209 * of type string carrying the whole query string.
1210 */
1211static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1212{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001213 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001214 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001215 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001216 char *ptr, *end;
1217
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001218 if (!htx)
1219 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001220
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001221 sl = http_get_stline(htx);
1222 ptr = HTX_SL_REQ_UPTR(sl);
1223 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001224
1225 /* look up the '?' */
1226 do {
1227 if (ptr == end)
1228 return 0;
1229 } while (*ptr++ != '?');
1230
1231 smp->data.type = SMP_T_STR;
1232 smp->data.u.str.area = ptr;
1233 smp->data.u.str.data = end - ptr;
1234 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1235 return 1;
1236}
1237
1238static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1239{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001240 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001241 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001242
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001243 if (!htx)
1244 return 0;
1245 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001246 smp->data.u.sint = 1;
1247 return 1;
1248}
1249
1250/* return a valid test if the current request is the first one on the connection */
1251static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1252{
Willy Tarreau79512b62020-04-29 11:52:13 +02001253 if (!smp->strm)
1254 return 0;
1255
Willy Tarreau79e57332018-10-02 16:01:16 +02001256 smp->data.type = SMP_T_BOOL;
1257 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1258 return 1;
1259}
1260
Christopher Fauleta4063562019-08-02 11:51:37 +02001261/* Fetch the authentication method if there is an Authorization header. It
1262 * relies on get_http_auth()
1263 */
1264static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1265{
1266 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001267 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001268 struct http_txn *txn;
1269
1270 if (!htx)
1271 return 0;
1272
1273 txn = smp->strm->txn;
1274 if (!get_http_auth(smp, htx))
1275 return 0;
1276
1277 switch (txn->auth.method) {
1278 case HTTP_AUTH_BASIC:
1279 smp->data.u.str.area = "Basic";
1280 smp->data.u.str.data = 5;
1281 break;
1282 case HTTP_AUTH_DIGEST:
1283 /* Unexpected because not supported */
1284 smp->data.u.str.area = "Digest";
1285 smp->data.u.str.data = 6;
1286 break;
1287 default:
1288 return 0;
1289 }
1290
1291 smp->data.type = SMP_T_STR;
1292 smp->flags = SMP_F_CONST;
1293 return 1;
1294}
1295
1296/* Fetch the user supplied if there is an Authorization header. It relies on
1297 * get_http_auth()
1298 */
1299static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1300{
1301 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001302 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001303 struct http_txn *txn;
1304
1305 if (!htx)
1306 return 0;
1307
1308 txn = smp->strm->txn;
1309 if (!get_http_auth(smp, htx))
1310 return 0;
1311
1312 smp->data.type = SMP_T_STR;
1313 smp->data.u.str.area = txn->auth.user;
1314 smp->data.u.str.data = strlen(txn->auth.user);
1315 smp->flags = SMP_F_CONST;
1316 return 1;
1317}
1318
1319/* Fetch the password supplied if there is an Authorization header. It relies on
1320 * get_http_auth()
1321 */
1322static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1323{
1324 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001325 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001326 struct http_txn *txn;
1327
1328 if (!htx)
1329 return 0;
1330
1331 txn = smp->strm->txn;
1332 if (!get_http_auth(smp, htx))
1333 return 0;
1334
1335 smp->data.type = SMP_T_STR;
1336 smp->data.u.str.area = txn->auth.pass;
1337 smp->data.u.str.data = strlen(txn->auth.pass);
1338 smp->flags = SMP_F_CONST;
1339 return 1;
1340}
1341
Willy Tarreau79e57332018-10-02 16:01:16 +02001342/* Accepts exactly 1 argument of type userlist */
1343static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1344{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001345 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001346 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001347
Christopher Faulet623af932021-01-29 11:22:15 +01001348 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001349 return 0;
1350
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001351 if (!htx)
1352 return 0;
1353 if (!get_http_auth(smp, htx))
1354 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001355
1356 smp->data.type = SMP_T_BOOL;
1357 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001358 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001359 return 1;
1360}
1361
1362/* Accepts exactly 1 argument of type userlist */
1363static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1364{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001365 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001366 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001367
Christopher Faulet623af932021-01-29 11:22:15 +01001368 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001369 return 0;
1370
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001371 if (!htx)
1372 return 0;
1373 if (!get_http_auth(smp, htx))
1374 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001375
Willy Tarreau79e57332018-10-02 16:01:16 +02001376 /* if the user does not belong to the userlist or has a wrong password,
1377 * report that it unconditionally does not match. Otherwise we return
1378 * a string containing the username.
1379 */
1380 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1381 smp->strm->txn->auth.pass))
1382 return 0;
1383
1384 /* pat_match_auth() will need the user list */
1385 smp->ctx.a[0] = args->data.usr;
1386
1387 smp->data.type = SMP_T_STR;
1388 smp->flags = SMP_F_CONST;
1389 smp->data.u.str.area = smp->strm->txn->auth.user;
1390 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1391
1392 return 1;
1393}
1394
1395/* Fetch a captured HTTP request header. The index is the position of
1396 * the "capture" option in the configuration file
1397 */
1398static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1399{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001400 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001401 int idx;
1402
Christopher Faulet623af932021-01-29 11:22:15 +01001403 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001404 return 0;
1405
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001406 if (!smp->strm)
1407 return 0;
1408
1409 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001410 idx = args->data.sint;
1411
1412 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1413 return 0;
1414
1415 smp->data.type = SMP_T_STR;
1416 smp->flags |= SMP_F_CONST;
1417 smp->data.u.str.area = smp->strm->req_cap[idx];
1418 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1419
1420 return 1;
1421}
1422
1423/* Fetch a captured HTTP response header. The index is the position of
1424 * the "capture" option in the configuration file
1425 */
1426static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1427{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001428 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001429 int idx;
1430
Christopher Faulet623af932021-01-29 11:22:15 +01001431 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001432 return 0;
1433
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001434 if (!smp->strm)
1435 return 0;
1436
1437 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001438 idx = args->data.sint;
1439
1440 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1441 return 0;
1442
1443 smp->data.type = SMP_T_STR;
1444 smp->flags |= SMP_F_CONST;
1445 smp->data.u.str.area = smp->strm->res_cap[idx];
1446 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1447
1448 return 1;
1449}
1450
1451/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1452static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1453{
1454 struct buffer *temp;
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001455 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001456 char *ptr;
1457
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001458 if (!smp->strm)
1459 return 0;
1460
1461 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001462 if (!txn || !txn->uri)
1463 return 0;
1464
1465 ptr = txn->uri;
1466
1467 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1468 ptr++;
1469
1470 temp = get_trash_chunk();
1471 temp->area = txn->uri;
1472 temp->data = ptr - txn->uri;
1473 smp->data.u.str = *temp;
1474 smp->data.type = SMP_T_STR;
1475 smp->flags = SMP_F_CONST;
1476
1477 return 1;
1478
1479}
1480
1481/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1482static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1483{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001484 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001485 struct ist path;
1486 const char *ptr;
1487
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001488 if (!smp->strm)
1489 return 0;
1490
1491 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001492 if (!txn || !txn->uri)
1493 return 0;
1494
1495 ptr = txn->uri;
1496
1497 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1498 ptr++;
1499
1500 if (!*ptr)
1501 return 0;
1502
Christopher Faulet78337bb2018-11-15 14:35:18 +01001503 /* skip the first space and find space after URI */
1504 path = ist2(++ptr, 0);
1505 while (*ptr != ' ' && *ptr != '\0')
1506 ptr++;
1507 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001508
Christopher Faulet78337bb2018-11-15 14:35:18 +01001509 path = http_get_path(path);
Tim Duesterhused526372020-03-05 17:56:33 +01001510 if (!isttest(path))
Willy Tarreau79e57332018-10-02 16:01:16 +02001511 return 0;
1512
1513 smp->data.u.str.area = path.ptr;
1514 smp->data.u.str.data = path.len;
1515 smp->data.type = SMP_T_STR;
1516 smp->flags = SMP_F_CONST;
1517
1518 return 1;
1519}
1520
1521/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1522 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1523 */
1524static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1525{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001526 struct http_txn *txn;
1527
1528 if (!smp->strm)
1529 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001530
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001531 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001532 if (!txn || txn->req.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001533 return 0;
1534
1535 if (txn->req.flags & HTTP_MSGF_VER_11)
1536 smp->data.u.str.area = "HTTP/1.1";
1537 else
1538 smp->data.u.str.area = "HTTP/1.0";
1539
1540 smp->data.u.str.data = 8;
1541 smp->data.type = SMP_T_STR;
1542 smp->flags = SMP_F_CONST;
1543 return 1;
1544
1545}
1546
1547/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1548 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1549 */
1550static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1551{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001552 struct http_txn *txn;
1553
1554 if (!smp->strm)
1555 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001556
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001557 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001558 if (!txn || txn->rsp.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001559 return 0;
1560
1561 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1562 smp->data.u.str.area = "HTTP/1.1";
1563 else
1564 smp->data.u.str.area = "HTTP/1.0";
1565
1566 smp->data.u.str.data = 8;
1567 smp->data.type = SMP_T_STR;
1568 smp->flags = SMP_F_CONST;
1569 return 1;
1570
1571}
1572
1573/* Iterate over all cookies present in a message. The context is stored in
1574 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1575 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1576 * the direction, multiple cookies may be parsed on the same line or not.
Maciej Zdebdea7c202020-11-13 09:38:06 +00001577 * If provided, the searched cookie name is in args, in args->data.str. If
1578 * the input options indicate that no iterating is desired, then only last
1579 * value is fetched if any. If no cookie name is provided, the first cookie
1580 * value found is fetched. The returned sample is of type CSTR. Can be used
1581 * to parse cookies in other files.
Willy Tarreau79e57332018-10-02 16:01:16 +02001582 */
1583static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1584{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001585 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1586 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001587 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001588 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001589 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1590 struct ist hdr;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001591 char *cook = NULL;
1592 size_t cook_l = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001593 int found = 0;
1594
Christopher Faulet623af932021-01-29 11:22:15 +01001595 if (args->type == ARGT_STR) {
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001596 cook = args->data.str.area;
1597 cook_l = args->data.str.data;
1598 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001599
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001600 if (!ctx) {
1601 /* first call */
1602 ctx = &static_http_hdr_ctx;
1603 ctx->blk = NULL;
1604 smp->ctx.a[2] = ctx;
1605 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001606
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001607 if (!htx)
1608 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001609
Christopher Faulet16032ab2020-04-30 11:30:00 +02001610 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001611
Maciej Zdebdea7c202020-11-13 09:38:06 +00001612 /* OK so basically here, either we want only one value or we want to
1613 * iterate over all of them and we fetch the next one. In this last case
1614 * SMP_OPT_ITERATE option is set.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001615 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001616
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001617 if (!(smp->flags & SMP_F_NOT_LAST)) {
1618 /* search for the header from the beginning, we must first initialize
1619 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001620 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001621 smp->ctx.a[0] = NULL;
1622 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001623 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001624
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001625 smp->flags |= SMP_F_VOL_HDR;
1626 while (1) {
1627 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1628 if (!smp->ctx.a[0]) {
1629 if (!http_find_header(htx, hdr, ctx, 0))
1630 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001631
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001632 if (ctx->value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001633 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001634
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001635 smp->ctx.a[0] = ctx->value.ptr;
1636 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001637 }
1638
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001639 smp->data.type = SMP_T_STR;
1640 smp->flags |= SMP_F_CONST;
1641 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001642 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001643 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1644 &smp->data.u.str.area,
1645 &smp->data.u.str.data);
1646 if (smp->ctx.a[0]) {
1647 found = 1;
Maciej Zdebdea7c202020-11-13 09:38:06 +00001648 if (smp->opt & SMP_OPT_ITERATE) {
1649 /* iterate on cookie value */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001650 smp->flags |= SMP_F_NOT_LAST;
1651 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001652 }
Maciej Zdebdea7c202020-11-13 09:38:06 +00001653 if (args->data.str.data == 0) {
1654 /* No cookie name, first occurrence returned */
1655 break;
1656 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001657 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001658 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001659 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001660
Willy Tarreau79e57332018-10-02 16:01:16 +02001661 /* all cookie headers and values were scanned. If we're looking for the
1662 * last occurrence, we may return it now.
1663 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001664 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001665 smp->flags &= ~SMP_F_NOT_LAST;
1666 return found;
1667}
1668
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001669/* Same than smp_fetch_cookie() but only relies on the sample direction to
1670 * choose the right channel. So instead of duplicating the code, we just change
1671 * the keyword and then fallback on smp_fetch_cookie().
1672 */
1673static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1674{
1675 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1676 return smp_fetch_cookie(args, smp, kw, private);
1677}
1678
Willy Tarreau79e57332018-10-02 16:01:16 +02001679/* Iterate over all cookies present in a request to count how many occurrences
1680 * match the name in args and args->data.str.len. If <multi> is non-null, then
1681 * multiple cookies may be parsed on the same line. The returned sample is of
1682 * type UINT. Accepts exactly 1 argument of type string.
1683 */
1684static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1685{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001686 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1687 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001688 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001689 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001690 struct http_hdr_ctx ctx;
1691 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001692 char *val_beg, *val_end;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001693 char *cook = NULL;
1694 size_t cook_l = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001695 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001696
Christopher Faulet623af932021-01-29 11:22:15 +01001697 if (args->type == ARGT_STR){
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001698 cook = args->data.str.area;
1699 cook_l = args->data.str.data;
1700 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001701
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001702 if (!htx)
1703 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001704
Christopher Faulet16032ab2020-04-30 11:30:00 +02001705 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001706
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001707 val_end = val_beg = NULL;
1708 ctx.blk = NULL;
1709 cnt = 0;
1710 while (1) {
1711 /* Note: val_beg == NULL every time we need to fetch a new header */
1712 if (!val_beg) {
1713 if (!http_find_header(htx, hdr, &ctx, 0))
1714 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001715
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001716 if (ctx.value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001717 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001718
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001719 val_beg = ctx.value.ptr;
1720 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001721 }
1722
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001723 smp->data.type = SMP_T_STR;
1724 smp->flags |= SMP_F_CONST;
1725 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001726 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001727 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1728 &smp->data.u.str.area,
1729 &smp->data.u.str.data))) {
1730 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001731 }
1732 }
1733
1734 smp->data.type = SMP_T_SINT;
1735 smp->data.u.sint = cnt;
1736 smp->flags |= SMP_F_VOL_HDR;
1737 return 1;
1738}
1739
1740/* Fetch an cookie's integer value. The integer value is returned. It
1741 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1742 */
1743static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1744{
1745 int ret = smp_fetch_cookie(args, smp, kw, private);
1746
1747 if (ret > 0) {
1748 smp->data.type = SMP_T_SINT;
1749 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1750 smp->data.u.str.data);
1751 }
1752
1753 return ret;
1754}
1755
1756/************************************************************************/
1757/* The code below is dedicated to sample fetches */
1758/************************************************************************/
1759
1760/* This scans a URL-encoded query string. It takes an optionally wrapping
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001761 * string whose first contiguous chunk has its beginning in ctx->a[0] and end
Willy Tarreau79e57332018-10-02 16:01:16 +02001762 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1763 * pointers are updated for next iteration before leaving.
1764 */
1765static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1766{
1767 const char *vstart, *vend;
1768 struct buffer *temp;
1769 const char **chunks = (const char **)smp->ctx.a;
1770
1771 if (!http_find_next_url_param(chunks, name, name_len,
1772 &vstart, &vend, delim))
1773 return 0;
1774
1775 /* Create sample. If the value is contiguous, return the pointer as CONST,
1776 * if the value is wrapped, copy-it in a buffer.
1777 */
1778 smp->data.type = SMP_T_STR;
1779 if (chunks[2] &&
1780 vstart >= chunks[0] && vstart <= chunks[1] &&
1781 vend >= chunks[2] && vend <= chunks[3]) {
1782 /* Wrapped case. */
1783 temp = get_trash_chunk();
1784 memcpy(temp->area, vstart, chunks[1] - vstart);
1785 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1786 vend - chunks[2]);
1787 smp->data.u.str.area = temp->area;
1788 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1789 } else {
1790 /* Contiguous case. */
1791 smp->data.u.str.area = (char *)vstart;
1792 smp->data.u.str.data = vend - vstart;
1793 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1794 }
1795
1796 /* Update context, check wrapping. */
1797 chunks[0] = vend;
1798 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1799 chunks[1] = chunks[3];
1800 chunks[2] = NULL;
1801 }
1802
1803 if (chunks[0] < chunks[1])
1804 smp->flags |= SMP_F_NOT_LAST;
1805
1806 return 1;
1807}
1808
1809/* This function iterates over each parameter of the query string. It uses
1810 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1811 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1812 * An optional parameter name is passed in args[0], otherwise any parameter is
1813 * considered. It supports an optional delimiter argument for the beginning of
1814 * the string in args[1], which defaults to "?".
1815 */
1816static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1817{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001818 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001819 char delim = '?';
1820 const char *name;
1821 int name_len;
1822
Christopher Faulet623af932021-01-29 11:22:15 +01001823 if ((args[0].type && args[0].type != ARGT_STR) ||
Willy Tarreau79e57332018-10-02 16:01:16 +02001824 (args[1].type && args[1].type != ARGT_STR))
1825 return 0;
1826
1827 name = "";
1828 name_len = 0;
1829 if (args->type == ARGT_STR) {
1830 name = args->data.str.area;
1831 name_len = args->data.str.data;
1832 }
1833
1834 if (args[1].type)
1835 delim = *args[1].data.str.area;
1836
1837 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001838 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001839 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001840
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001841 if (!htx)
1842 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001843
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001844 sl = http_get_stline(htx);
1845 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1846 if (!smp->ctx.a[0])
1847 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001848
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001849 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001850
1851 /* Assume that the context is filled with NULL pointer
1852 * before the first call.
1853 * smp->ctx.a[2] = NULL;
1854 * smp->ctx.a[3] = NULL;
1855 */
1856 }
1857
1858 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1859}
1860
1861/* This function iterates over each parameter of the body. This requires
1862 * that the body has been waited for using http-buffer-request. It uses
1863 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001864 * contiguous part of the body, and optionally ctx->a[2..3] to reference the
Willy Tarreau79e57332018-10-02 16:01:16 +02001865 * optional second part if the body wraps at the end of the buffer. An optional
1866 * parameter name is passed in args[0], otherwise any parameter is considered.
1867 */
1868static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1869{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001870 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001871 const char *name;
1872 int name_len;
1873
Christopher Faulet623af932021-01-29 11:22:15 +01001874 if (args[0].type && args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001875 return 0;
1876
1877 name = "";
1878 name_len = 0;
1879 if (args[0].type == ARGT_STR) {
1880 name = args[0].data.str.area;
1881 name_len = args[0].data.str.data;
1882 }
1883
1884 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulete596d182020-05-05 17:46:34 +02001885 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001886 struct buffer *temp;
1887 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001888
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001889 if (!htx)
1890 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001891
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001892 temp = get_trash_chunk();
1893 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1894 struct htx_blk *blk = htx_get_blk(htx, pos);
1895 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001896
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001897 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001898 break;
1899 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001900 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001901 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001902 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001903 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001904
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001905 smp->ctx.a[0] = temp->area;
1906 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001907
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001908 /* Assume that the context is filled with NULL pointer
1909 * before the first call.
1910 * smp->ctx.a[2] = NULL;
1911 * smp->ctx.a[3] = NULL;
1912 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001913
Willy Tarreau79e57332018-10-02 16:01:16 +02001914 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001915
Willy Tarreau79e57332018-10-02 16:01:16 +02001916 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1917}
1918
1919/* Return the signed integer value for the specified url parameter (see url_param
1920 * above).
1921 */
1922static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1923{
1924 int ret = smp_fetch_url_param(args, smp, kw, private);
1925
1926 if (ret > 0) {
1927 smp->data.type = SMP_T_SINT;
1928 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1929 smp->data.u.str.data);
1930 }
1931
1932 return ret;
1933}
1934
1935/* This produces a 32-bit hash of the concatenation of the first occurrence of
1936 * the Host header followed by the path component if it begins with a slash ('/').
1937 * This means that '*' will not be added, resulting in exactly the first Host
1938 * entry. If no Host header is found, then the path is used. The resulting value
1939 * is hashed using the url hash followed by a full avalanche hash and provides a
1940 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1941 * high-traffic sites without having to store whole paths.
1942 * this differs from the base32 functions in that it includes the url parameters
1943 * as well as the path
1944 */
1945static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1946{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001947 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001948 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001949 struct http_hdr_ctx ctx;
1950 struct htx_sl *sl;
1951 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001952 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001953
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001954 if (!htx)
1955 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001956
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001957 ctx.blk = NULL;
1958 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1959 /* OK we have the header value in ctx.value */
1960 while (ctx.value.len--)
1961 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001962 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001963
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001964 /* now retrieve the path */
1965 sl = http_get_stline(htx);
1966 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001967 if (path.len && *(path.ptr) == '/') {
1968 while (path.len--)
1969 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001970 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001971
Willy Tarreau79e57332018-10-02 16:01:16 +02001972 hash = full_hash(hash);
1973
1974 smp->data.type = SMP_T_SINT;
1975 smp->data.u.sint = hash;
1976 smp->flags = SMP_F_VOL_1ST;
1977 return 1;
1978}
1979
1980/* This concatenates the source address with the 32-bit hash of the Host and
1981 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1982 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1983 * on the source address length. The URL hash is stored before the address so
1984 * that in environments where IPv6 is insignificant, truncating the output to
1985 * 8 bytes would still work.
1986 */
1987static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1988{
1989 struct buffer *temp;
1990 struct connection *cli_conn = objt_conn(smp->sess->origin);
1991
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001992 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001993 return 0;
1994
1995 if (!smp_fetch_url32(args, smp, kw, private))
1996 return 0;
1997
1998 temp = get_trash_chunk();
1999 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2000 temp->data += sizeof(unsigned int);
2001
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002002 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02002003 case AF_INET:
2004 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002005 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002006 4);
2007 temp->data += 4;
2008 break;
2009 case AF_INET6:
2010 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002011 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002012 16);
2013 temp->data += 16;
2014 break;
2015 default:
2016 return 0;
2017 }
2018
2019 smp->data.u.str = *temp;
2020 smp->data.type = SMP_T_BIN;
2021 return 1;
2022}
2023
2024/************************************************************************/
2025/* Other utility functions */
2026/************************************************************************/
2027
2028/* This function is used to validate the arguments passed to any "hdr" fetch
2029 * keyword. These keywords support an optional positive or negative occurrence
2030 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2031 * is assumed that the types are already the correct ones. Returns 0 on error,
2032 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2033 * error message in case of error, that the caller is responsible for freeing.
2034 * The initial location must either be freeable or NULL.
2035 * Note: this function's pointer is checked from Lua.
2036 */
2037int val_hdr(struct arg *arg, char **err_msg)
2038{
2039 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2040 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2041 return 0;
2042 }
2043 return 1;
2044}
2045
2046/************************************************************************/
2047/* All supported sample fetch keywords must be declared here. */
2048/************************************************************************/
2049
2050/* Note: must not be declared <const> as its list will be overwritten */
2051static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2052 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2053 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2054 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Yves Lafonb4d37082021-02-11 11:01:28 +01002055 { "baseq", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002056
2057 /* capture are allocated and are permanent in the stream */
2058 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2059
2060 /* retrieve these captures from the HTTP logs */
2061 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2062 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2063 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2064
2065 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2066 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2067
2068 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2069 * are only here to match the ACL's name, are request-only and are used
2070 * for ACL compatibility only.
2071 */
2072 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002073 { "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 +02002074 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2075 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2076
2077 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2078 * only here to match the ACL's name, are request-only and are used for
2079 * ACL compatibility only.
2080 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002081 { "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 +02002082 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2083 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2084 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2085
Christopher Fauleta4063562019-08-02 11:51:37 +02002086 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2087 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2088 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002089 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2090 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2091 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2092 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2093 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Faulete720c322020-09-02 17:25:18 +02002094 { "pathq", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002095 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2096
2097 /* HTTP protocol on the request path */
2098 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2099 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2100
2101 /* HTTP version on the request path */
2102 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2103 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2104
2105 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2106 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2107 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2108 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2109
2110 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2111 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2112
2113 /* HTTP version on the response path */
2114 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2115 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2116
Christopher Faulete596d182020-05-05 17:46:34 +02002117 { "res.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2118 { "res.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2119 { "res.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2120
2121 { "res.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2122 { "res.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2123
Willy Tarreau79e57332018-10-02 16:01:16 +02002124 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2125 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2126 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2127 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2128
2129 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2130 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2131 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2132 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2133 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2134 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2135 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2136
2137 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2138 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2139 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2140 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2141
2142 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2143 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2144 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2145 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2146 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2147 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2148 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2149
2150 /* scook is valid only on the response and is used for ACL compatibility */
2151 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2152 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2153 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2154 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2155
2156 /* shdr is valid only on the response and is used for ACL compatibility */
2157 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2158 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2159 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2160 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2161
2162 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2163 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2164 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2165 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2166 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2167 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2168 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2169 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2170 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2171 { "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 +02002172
Willy Tarreau79e57332018-10-02 16:01:16 +02002173 { /* END */ },
2174}};
2175
Willy Tarreau0108d902018-11-25 19:14:37 +01002176INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002177
2178/*
2179 * Local variables:
2180 * c-indent-level: 8
2181 * c-basic-offset: 8
2182 * End:
2183 */