blob: a28cbaf4b3549802c348548997b29f9532a7fc67 [file] [log] [blame]
Willy Tarreau79e57332018-10-02 16:01:16 +02001/*
2 * HTTP samples fetching
3 *
4 * Copyright 2000-2018 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/types.h>
14
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
18
Willy Tarreaub2551052020-06-09 09:07:15 +020019#include <haproxy/api.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020020#include <haproxy/arg.h>
Willy Tarreauac13aea2020-06-04 10:36:03 +020021#include <haproxy/auth.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020022#include <haproxy/base64.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020023#include <haproxy/channel.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020024#include <haproxy/chunk.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020025#include <haproxy/connection.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020026#include <haproxy/global.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020027#include <haproxy/h1.h>
Willy Tarreauc6fe8842020-06-04 09:00:02 +020028#include <haproxy/h1_htx.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020029#include <haproxy/http.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020030#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020031#include <haproxy/http_fetch.h>
Willy Tarreau87735332020-06-04 09:08:41 +020032#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020033#include <haproxy/htx.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020034#include <haproxy/obj_type.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020035#include <haproxy/pool.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020036#include <haproxy/sample.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/stream.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020038#include <haproxy/tools.h>
Willy Tarreaud6788052020-05-27 15:59:00 +020039#include <haproxy/version.h>
Willy Tarreau79e57332018-10-02 16:01:16 +020040
Willy Tarreau79e57332018-10-02 16:01:16 +020041
42/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
Christopher Fauletef453ed2018-10-24 21:39:27 +020043static THREAD_LOCAL struct http_hdr_ctx static_http_hdr_ctx;
Richard Russo458eafb2019-07-31 11:45:56 -070044/* this is used to convert raw connection buffers to htx */
45static THREAD_LOCAL struct buffer static_raw_htx_chunk;
46static THREAD_LOCAL char *static_raw_htx_buf;
Christopher Fauletef453ed2018-10-24 21:39:27 +020047
Christopher Faulet89dc4992019-04-17 12:02:59 +020048#define SMP_REQ_CHN(smp) (smp->strm ? &smp->strm->req : NULL)
49#define SMP_RES_CHN(smp) (smp->strm ? &smp->strm->res : NULL)
Willy Tarreau79e57332018-10-02 16:01:16 +020050
Richard Russo458eafb2019-07-31 11:45:56 -070051/* This function returns the static htx chunk, where raw connections get
52 * converted to HTX as needed for samplxsing.
53 */
54struct buffer *get_raw_htx_chunk(void)
55{
56 chunk_reset(&static_raw_htx_chunk);
57 return &static_raw_htx_chunk;
58}
59
60static int alloc_raw_htx_chunk_per_thread()
61{
62 static_raw_htx_buf = malloc(global.tune.bufsize);
63 if (!static_raw_htx_buf)
64 return 0;
65 chunk_init(&static_raw_htx_chunk, static_raw_htx_buf, global.tune.bufsize);
66 return 1;
67}
68
69static void free_raw_htx_chunk_per_thread()
70{
Willy Tarreau61cfdf42021-02-20 10:46:51 +010071 ha_free(&static_raw_htx_buf);
Richard Russo458eafb2019-07-31 11:45:56 -070072}
73
74REGISTER_PER_THREAD_ALLOC(alloc_raw_htx_chunk_per_thread);
75REGISTER_PER_THREAD_FREE(free_raw_htx_chunk_per_thread);
76
Willy Tarreau79e57332018-10-02 16:01:16 +020077/*
78 * Returns the data from Authorization header. Function may be called more
79 * than once so data is stored in txn->auth_data. When no header is found
80 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
81 * searching again for something we are unable to find anyway. However, if
82 * the result if valid, the cache is not reused because we would risk to
83 * have the credentials overwritten by another stream in parallel.
Willy Tarreaueae83722020-04-29 11:52:51 +020084 * The caller is responsible for passing a sample with a valid stream/txn,
85 * and a valid htx.
Willy Tarreau79e57332018-10-02 16:01:16 +020086 */
87
Christopher Fauletcd761952019-07-15 13:58:29 +020088static int get_http_auth(struct sample *smp, struct htx *htx)
Willy Tarreau79e57332018-10-02 16:01:16 +020089{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020090 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020091 struct http_txn *txn = s->txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020092 struct http_hdr_ctx ctx = { .blk = NULL };
93 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020094 struct buffer auth_method;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020095 char *p;
Willy Tarreau79e57332018-10-02 16:01:16 +020096 int len;
97
98#ifdef DEBUG_AUTH
99 printf("Auth for stream %p: %d\n", s, txn->auth.method);
100#endif
Willy Tarreau79e57332018-10-02 16:01:16 +0200101 if (txn->auth.method == HTTP_AUTH_WRONG)
102 return 0;
103
104 txn->auth.method = HTTP_AUTH_WRONG;
105
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200106 if (txn->flags & TX_USE_PX_CONN)
107 hdr = ist("Proxy-Authorization");
108 else
109 hdr = ist("Authorization");
Willy Tarreau79e57332018-10-02 16:01:16 +0200110
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200111 ctx.blk = NULL;
112 if (!http_find_header(htx, hdr, &ctx, 0))
113 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200114
Willy Tarreau17254932020-09-02 07:08:47 +0200115 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
116 if (!p || p == ctx.value.ptr) /* if no space was found or if the space is the first character */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200117 return 0;
Willy Tarreau17254932020-09-02 07:08:47 +0200118 len = p - ctx.value.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +0200119
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200120 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
121 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200122
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200123 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200124
125 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
126 struct buffer *http_auth = get_trash_chunk();
127
128 len = base64dec(txn->auth.method_data.area,
129 txn->auth.method_data.data,
130 http_auth->area, global.tune.bufsize - 1);
131
132 if (len < 0)
133 return 0;
134
135
136 http_auth->area[len] = '\0';
137
138 p = strchr(http_auth->area, ':');
139
140 if (!p)
141 return 0;
142
143 txn->auth.user = http_auth->area;
144 *p = '\0';
145 txn->auth.pass = p+1;
146
147 txn->auth.method = HTTP_AUTH_BASIC;
148 return 1;
149 }
150
151 return 0;
152}
153
154/* This function ensures that the prerequisites for an L7 fetch are ready,
155 * which means that a request or response is ready. If some data is missing,
156 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200157 * to extract data from L7. If <vol> is non-null during a prefetch, another
158 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200159 *
160 * The function returns :
161 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
162 * decide whether or not an HTTP message is present ;
163 * NULL if the requested data cannot be fetched or if it is certain that
Willy Tarreaueae83722020-04-29 11:52:51 +0200164 * we'll never have any HTTP message there; this includes null strm or chn.
Willy Tarreaua6d98792020-08-12 14:04:52 +0200165 * NULL if the sample's direction does not match the channel's (i.e. the
166 * function was asked to work on the wrong channel)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200167 * The HTX message if ready
168 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200169struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, struct check *check, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200170{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200171 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200172 struct http_txn *txn = NULL;
173 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200174 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100175 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200176
Willy Tarreaua6d98792020-08-12 14:04:52 +0200177 if (chn &&
178 (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ && (chn->flags & CF_ISRESP)) ||
179 ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES && !(chn->flags & CF_ISRESP))))
180 return 0;
181
Christopher Fauletef453ed2018-10-24 21:39:27 +0200182 /* Note: it is possible that <s> is NULL when called before stream
183 * initialization (eg: tcp-request connection), so this function is the
184 * one responsible for guarding against this case for all HTTP users.
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200185 *
186 * In the health check context, the stream and the channel must be NULL
187 * and <check> must be set. In this case, only the input buffer,
188 * corresponding to the response, is considered. It is the caller
189 * responsibility to provide <check>.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200190 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200191 BUG_ON(check && (s || chn));
192 if (!s || !chn) {
193 if (check) {
194 htx = htxbuf(&check->bi);
195
196 /* Analyse not yet started */
197 if (htx_is_empty(htx) || htx->first == -1)
198 return NULL;
199
200 sl = http_get_stline(htx);
201 if (vol && !sl) {
202 /* The start-line was already forwarded, it is too late to fetch anything */
203 return NULL;
204 }
205 goto end;
206 }
207
Christopher Fauletef453ed2018-10-24 21:39:27 +0200208 return NULL;
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200209 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200210
Christopher Faulet75f619a2021-03-08 19:12:58 +0100211 if (!s->txn && !http_create_txn(s))
212 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200213 txn = s->txn;
214 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200215
Christopher Fauleteca88542019-04-03 10:12:42 +0200216 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200217 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200218
Christopher Faulet89dc4992019-04-17 12:02:59 +0200219 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
220 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200221
Christopher Faulet89dc4992019-04-17 12:02:59 +0200222 if (msg->msg_state < HTTP_MSG_BODY) {
223 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200224 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200225 /* Parsing is done by the mux, just wait */
226 smp->flags |= SMP_F_MAY_CHANGE;
227 return NULL;
228 }
229 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200230 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200231 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200232 /* The start-line was already forwarded, it is too late to fetch anything */
233 return NULL;
234 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200235 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200236 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200237 struct buffer *buf;
238 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200239 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200240 union h1_sl h1sl;
241 unsigned int flags = HTX_FL_NONE;
242 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200243
Christopher Faulet89dc4992019-04-17 12:02:59 +0200244 /* no HTTP fetch on the response in TCP mode */
245 if (chn->flags & CF_ISRESP)
246 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200247
Christopher Faulet89dc4992019-04-17 12:02:59 +0200248 /* Now we are working on the request only */
249 buf = &chn->buf;
250 if (b_head(buf) + b_data(buf) > b_wrap(buf))
251 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200252
Christopher Faulet89dc4992019-04-17 12:02:59 +0200253 h1m_init_req(&h1m);
254 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
255 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
256 if (ret <= 0) {
257 /* Invalid or too big*/
258 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200259 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100260
Christopher Faulet89dc4992019-04-17 12:02:59 +0200261 /* wait for a full request */
262 smp->flags |= SMP_F_MAY_CHANGE;
263 return NULL;
264 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100265
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500266 /* OK we just got a valid HTTP message. We have to convert it
Christopher Faulet89dc4992019-04-17 12:02:59 +0200267 * into an HTX message.
268 */
269 if (unlikely(h1sl.rq.v.len == 0)) {
270 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
271 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200272 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200273 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200274 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200275
276 /* Set HTX start-line flags */
277 if (h1m.flags & H1_MF_VER_11)
278 flags |= HTX_SL_F_VER_11;
279 if (h1m.flags & H1_MF_XFER_ENC)
280 flags |= HTX_SL_F_XFER_ENC;
281 flags |= HTX_SL_F_XFER_LEN;
282 if (h1m.flags & H1_MF_CHNK)
283 flags |= HTX_SL_F_CHNK;
284 else if (h1m.flags & H1_MF_CLEN)
285 flags |= HTX_SL_F_CLEN;
286
Richard Russo458eafb2019-07-31 11:45:56 -0700287 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200288 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
289 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200290 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200291 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200292 }
293
294 /* OK we just got a valid HTTP message. If not already done by
295 * HTTP analyzers, we have some minor preparation to perform so
296 * that further checks can rely on HTTP tests.
297 */
298 if (sl && msg->msg_state < HTTP_MSG_BODY) {
299 if (!(chn->flags & CF_ISRESP)) {
300 txn->meth = sl->info.req.meth;
301 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
302 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200303 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200304 else
305 txn->status = sl->info.res.status;
306 if (sl->flags & HTX_SL_F_VER_11)
307 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200308 }
309
310 /* everything's OK */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200311 end:
Christopher Fauletef453ed2018-10-24 21:39:27 +0200312 return htx;
313}
314
Willy Tarreau79e57332018-10-02 16:01:16 +0200315/* This function fetches the method of current HTTP request and stores
316 * it in the global pattern struct as a chunk. There are two possibilities :
317 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
318 * in <len> and <ptr> is NULL ;
319 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
320 * <len> to its length.
321 * This is intended to be used with pat_match_meth() only.
322 */
323static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
324{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200325 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200326 struct http_txn *txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200327 int meth;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200328
Willy Tarreaua6d98792020-08-12 14:04:52 +0200329 txn = smp->strm->txn;
330 if (!txn)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200331 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200332
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200333 meth = txn->meth;
334 smp->data.type = SMP_T_METH;
335 smp->data.u.meth.meth = meth;
336 if (meth == HTTP_METH_OTHER) {
Willy Tarreaua6d98792020-08-12 14:04:52 +0200337 struct htx *htx;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200338 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200339
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200340 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
341 /* ensure the indexes are not affected */
342 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200343 }
Willy Tarreaua6d98792020-08-12 14:04:52 +0200344
Christopher Faulet6f97a612021-04-15 09:28:02 +0200345 htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreaua6d98792020-08-12 14:04:52 +0200346 if (!htx)
347 return 0;
348
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200349 sl = http_get_stline(htx);
350 smp->flags |= SMP_F_CONST;
351 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
352 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200353 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200354 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200355 return 1;
356}
357
358static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
359{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200360 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200361 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200362 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200363 char *ptr;
364 int len;
365
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200366 if (!htx)
367 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200368
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200369 sl = http_get_stline(htx);
370 len = HTX_SL_REQ_VLEN(sl);
371 ptr = HTX_SL_REQ_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200372
373 while ((len-- > 0) && (*ptr++ != '/'));
374 if (len <= 0)
375 return 0;
376
377 smp->data.type = SMP_T_STR;
378 smp->data.u.str.area = ptr;
379 smp->data.u.str.data = len;
380
381 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
382 return 1;
383}
384
385static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
386{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200387 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200388 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200389 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200390 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200391 char *ptr;
392 int len;
393
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200394 if (!htx)
395 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200396
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200397 sl = http_get_stline(htx);
398 len = HTX_SL_RES_VLEN(sl);
399 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200400
401 while ((len-- > 0) && (*ptr++ != '/'));
402 if (len <= 0)
403 return 0;
404
405 smp->data.type = SMP_T_STR;
406 smp->data.u.str.area = ptr;
407 smp->data.u.str.data = len;
408
409 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
410 return 1;
411}
412
413/* 3. Check on Status Code. We manipulate integers here. */
414static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
415{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200416 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200417 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200418 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200419 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200420 char *ptr;
421 int len;
422
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200423 if (!htx)
424 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200425
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200426 sl = http_get_stline(htx);
427 len = HTX_SL_RES_CLEN(sl);
428 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200429
430 smp->data.type = SMP_T_SINT;
431 smp->data.u.sint = __strl2ui(ptr, len);
432 smp->flags = SMP_F_VOL_1ST;
433 return 1;
434}
435
436static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
437{
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100438 struct ist unique_id;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100439
Willy Tarreau79e57332018-10-02 16:01:16 +0200440 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
441 return 0;
442
Willy Tarreaua1062a42020-04-29 11:50:38 +0200443 if (!smp->strm)
444 return 0;
445
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100446 unique_id = stream_generate_unique_id(smp->strm, &smp->sess->fe->format_unique_id);
447 if (!isttest(unique_id))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100448 return 0;
449
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100450 smp->data.u.str.area = smp->strm->unique_id.ptr;
451 smp->data.u.str.data = smp->strm->unique_id.len;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100452 smp->data.type = SMP_T_STR;
Willy Tarreau79e57332018-10-02 16:01:16 +0200453 smp->flags = SMP_F_CONST;
454 return 1;
455}
456
457/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800458 * empty line which separes headers from the body. This is useful
459 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200460 */
461static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
462{
Christopher Faulete596d182020-05-05 17:46:34 +0200463 /* possible keywords: req.hdrs, res.hdrs */
464 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200465 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200466 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200467 struct buffer *temp;
468 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200469
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200470 if (!htx)
471 return 0;
472 temp = get_trash_chunk();
473 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
474 struct htx_blk *blk = htx_get_blk(htx, pos);
475 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200476
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200477 if (type == HTX_BLK_HDR) {
478 struct ist n = htx_get_blk_name(htx, blk);
479 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200480
Christopher Faulet53a899b2019-10-08 16:38:42 +0200481 if (!h1_format_htx_hdr(n, v, temp))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200482 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200483 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200484 else if (type == HTX_BLK_EOH) {
485 if (!chunk_memcat(temp, "\r\n", 2))
486 return 0;
487 break;
488 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200489 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200490 smp->data.type = SMP_T_STR;
491 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200492 return 1;
493}
494
495/* Returns the header request in a length/value encoded format.
496 * This is useful for exchanges with the SPOE.
497 *
498 * A "length value" is a multibyte code encoding numbers. It uses the
499 * SPOE format. The encoding is the following:
500 *
501 * Each couple "header name" / "header value" is composed
502 * like this:
503 * "length value" "header name bytes"
504 * "length value" "header value bytes"
505 * When the last header is reached, the header name and the header
506 * value are empty. Their length are 0
507 */
508static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
509{
Christopher Faulete596d182020-05-05 17:46:34 +0200510 /* possible keywords: req.hdrs_bin, res.hdrs_bin */
511 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200512 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200513 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200514 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200515 char *p, *end;
516 int32_t pos;
517 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200518
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200519 if (!htx)
520 return 0;
521 temp = get_trash_chunk();
522 p = temp->area;
523 end = temp->area + temp->size;
524 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
525 struct htx_blk *blk = htx_get_blk(htx, pos);
526 enum htx_blk_type type = htx_get_blk_type(blk);
527 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200528
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200529 if (type == HTX_BLK_HDR) {
530 n = htx_get_blk_name(htx,blk);
531 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200532
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200533 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200534 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200535 if (ret == -1)
536 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200537 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200538 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200539 memcpy(p, n.ptr, n.len);
540 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200541
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200542 /* encode the header value. */
543 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200544 if (ret == -1)
545 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200546 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200547 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200548 memcpy(p, v.ptr, v.len);
549 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200550
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200551 }
552 else if (type == HTX_BLK_EOH) {
553 /* encode the end of the header list with empty
554 * header name and header value.
555 */
556 ret = encode_varint(0, &p, end);
557 if (ret == -1)
558 return 0;
559 ret = encode_varint(0, &p, end);
560 if (ret == -1)
561 return 0;
562 break;
563 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200564 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200565
566 /* Initialise sample data which will be filled. */
567 smp->data.type = SMP_T_BIN;
568 smp->data.u.str.area = temp->area;
569 smp->data.u.str.data = p - temp->area;
570 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200571 return 1;
572}
573
574/* returns the longest available part of the body. This requires that the body
575 * has been waited for using http-buffer-request.
576 */
577static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
578{
Christopher Faulete596d182020-05-05 17:46:34 +0200579 /* possible keywords: req.body, res.body */
580 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200581 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200582 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200583 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200584 int32_t pos;
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100585 int finished = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200586
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200587 if (!htx)
588 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200589
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200590 temp = get_trash_chunk();
591 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
592 struct htx_blk *blk = htx_get_blk(htx, pos);
593 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200594
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100595 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) {
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100596 finished = 1;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200597 break;
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100598 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200599 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +0200600 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200601 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200602 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200603 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200604
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200605 smp->data.type = SMP_T_BIN;
606 smp->data.u.str = *temp;
607 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200608
Christopher Fauleta9ffc412020-11-25 08:08:08 +0100609 if (!finished && (check || (chn && !channel_full(chn, global.tune.maxrewrite) &&
610 !(chn->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)))))
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200611 smp->flags |= SMP_F_MAY_CHANGE;
612
Willy Tarreau79e57332018-10-02 16:01:16 +0200613 return 1;
614}
615
616
617/* returns the available length of the body. This requires that the body
618 * has been waited for using http-buffer-request.
619 */
620static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
621{
Christopher Faulete596d182020-05-05 17:46:34 +0200622 /* possible keywords: req.body_len, res.body_len */
623 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200624 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200625 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200626 int32_t pos;
627 unsigned long long len = 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100628
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200629 if (!htx)
630 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100631
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200632 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
633 struct htx_blk *blk = htx_get_blk(htx, pos);
634 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100635
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100636 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200637 break;
638 if (type == HTX_BLK_DATA)
639 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200640 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200641
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200642 smp->data.type = SMP_T_SINT;
643 smp->data.u.sint = len;
644 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200645 return 1;
646}
647
648
649/* returns the advertised length of the body, or the advertised size of the
650 * chunks available in the buffer. This requires that the body has been waited
651 * for using http-buffer-request.
652 */
653static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
654{
Christopher Faulete596d182020-05-05 17:46:34 +0200655 /* possible keywords: req.body_size, res.body_size */
656 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200657 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200658 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200659 int32_t pos;
660 unsigned long long len = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200661
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200662 if (!htx)
663 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100664
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200665 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
666 struct htx_blk *blk = htx_get_blk(htx, pos);
667 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100668
Christopher Fauletd1ac2b92020-12-02 19:12:22 +0100669 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200670 break;
671 if (type == HTX_BLK_DATA)
672 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200673 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200674 if (htx->extra != ULLONG_MAX)
675 len += htx->extra;
Willy Tarreau79e57332018-10-02 16:01:16 +0200676
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200677 smp->data.type = SMP_T_SINT;
678 smp->data.u.sint = len;
679 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200680 return 1;
681}
682
683
684/* 4. Check on URL/URI. A pointer to the URI is stored. */
685static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
686{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200687 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200688 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200689 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200690
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200691 if (!htx)
692 return 0;
693 sl = http_get_stline(htx);
694 smp->data.type = SMP_T_STR;
695 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
696 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
697 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200698 return 1;
699}
700
701static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
702{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200703 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200704 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200705 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200706 struct sockaddr_storage addr;
707
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200708 memset(&addr, 0, sizeof(addr));
709
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200710 if (!htx)
711 return 0;
712 sl = http_get_stline(htx);
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200713 if (url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL) < 0)
714 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200715
Willy Tarreau48584642021-05-09 10:32:54 +0200716 if (addr.ss_family != AF_INET)
Willy Tarreau79e57332018-10-02 16:01:16 +0200717 return 0;
718
719 smp->data.type = SMP_T_IPV4;
720 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
721 smp->flags = 0;
722 return 1;
723}
724
725static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
726{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200727 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200728 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200729 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200730 struct sockaddr_storage addr;
731
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200732 memset(&addr, 0, sizeof(addr));
733
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200734 if (!htx)
735 return 0;
736 sl = http_get_stline(htx);
Amaury Denoyellec89d5332021-05-10 11:23:34 +0200737 if (url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL) < 0)
738 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200739
Willy Tarreau48584642021-05-09 10:32:54 +0200740 if (addr.ss_family != AF_INET)
Willy Tarreau79e57332018-10-02 16:01:16 +0200741 return 0;
742
743 smp->data.type = SMP_T_SINT;
Willy Tarreau48584642021-05-09 10:32:54 +0200744 smp->data.u.sint = get_host_port(&addr);
Willy Tarreau79e57332018-10-02 16:01:16 +0200745 smp->flags = 0;
746 return 1;
747}
748
749/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
750 * Accepts an optional argument of type string containing the header field name,
751 * and an optional argument of type signed or unsigned integer to request an
752 * explicit occurrence of the header. Note that in the event of a missing name,
753 * headers are considered from the first one. It does not stop on commas and
754 * returns full lines instead (useful for User-Agent or Date for example).
755 */
756static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
757{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200758 /* possible keywords: req.fhdr, res.fhdr */
759 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200760 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200761 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200762 struct http_hdr_ctx *ctx = smp->ctx.a[0];
763 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200764 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200765
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200766 if (!ctx) {
767 /* first call */
768 ctx = &static_http_hdr_ctx;
769 ctx->blk = NULL;
770 smp->ctx.a[0] = ctx;
771 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200772
Christopher Faulet623af932021-01-29 11:22:15 +0100773 if (args[0].type != ARGT_STR)
774 return 0;
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100775 name = ist2(args[0].data.str.area, args[0].data.str.data);
Willy Tarreau79e57332018-10-02 16:01:16 +0200776
Christopher Faulet623af932021-01-29 11:22:15 +0100777 if (args[1].type == ARGT_SINT)
778 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200779
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200780 if (!htx)
781 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200782
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200783 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
784 /* search for header from the beginning */
785 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200786
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200787 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
788 /* no explicit occurrence and single fetch => last header by default */
789 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200790
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200791 if (!occ)
792 /* prepare to report multiple occurrences for ACL fetches */
793 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200794
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200795 smp->data.type = SMP_T_STR;
796 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
797 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
798 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200799 smp->flags &= ~SMP_F_NOT_LAST;
800 return 0;
801}
802
803/* 6. Check on HTTP header count. The number of occurrences is returned.
804 * Accepts exactly 1 argument of type string. It does not stop on commas and
805 * returns full lines instead (useful for User-Agent or Date for example).
806 */
807static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
808{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200809 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
810 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200811 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200812 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200813 struct http_hdr_ctx ctx;
814 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200815 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200816
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200817 if (!htx)
818 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200819
Christopher Faulet623af932021-01-29 11:22:15 +0100820 if (args->type == ARGT_STR) {
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100821 name = ist2(args->data.str.area, args->data.str.data);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200822 } else {
Tim Duesterhus68a088d2021-02-28 16:11:37 +0100823 name = IST_NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200824 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200825
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200826 ctx.blk = NULL;
827 cnt = 0;
828 while (http_find_header(htx, name, &ctx, 1))
829 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200830 smp->data.type = SMP_T_SINT;
831 smp->data.u.sint = cnt;
832 smp->flags = SMP_F_VOL_HDR;
833 return 1;
834}
835
836static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
837{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200838 /* possible keywords: req.hdr_names, res.hdr_names */
839 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200840 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200841 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200842 struct buffer *temp;
843 char del = ',';
844
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200845 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200846
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200847 if (!htx)
848 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200849
Christopher Faulet623af932021-01-29 11:22:15 +0100850 if (args->type == ARGT_STR)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200851 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200852
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200853 temp = get_trash_chunk();
854 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
855 struct htx_blk *blk = htx_get_blk(htx, pos);
856 enum htx_blk_type type = htx_get_blk_type(blk);
857 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200858
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200859 if (type == HTX_BLK_EOH)
860 break;
861 if (type != HTX_BLK_HDR)
862 continue;
863 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200864
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200865 if (temp->data)
866 temp->area[temp->data++] = del;
867 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200868 }
869
870 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200871 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200872 smp->flags = SMP_F_VOL_HDR;
873 return 1;
874}
875
876/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
877 * Accepts an optional argument of type string containing the header field name,
878 * and an optional argument of type signed or unsigned integer to request an
879 * explicit occurrence of the header. Note that in the event of a missing name,
880 * headers are considered from the first one.
881 */
882static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
883{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200884 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
885 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200886 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200887 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200888 struct http_hdr_ctx *ctx = smp->ctx.a[0];
889 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200890 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200891
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200892 if (!ctx) {
893 /* first call */
894 ctx = &static_http_hdr_ctx;
895 ctx->blk = NULL;
896 smp->ctx.a[0] = ctx;
897 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200898
Christopher Faulet623af932021-01-29 11:22:15 +0100899 if (args[0].type != ARGT_STR)
900 return 0;
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100901 name = ist2(args[0].data.str.area, args[0].data.str.data);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200902
Christopher Faulet623af932021-01-29 11:22:15 +0100903 if (args[1].type == ARGT_SINT)
904 occ = args[1].data.sint;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200905
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200906 if (!htx)
907 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200908
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200909 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
910 /* search for header from the beginning */
911 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200912
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200913 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
914 /* no explicit occurrence and single fetch => last header by default */
915 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200916
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200917 if (!occ)
918 /* prepare to report multiple occurrences for ACL fetches */
919 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200920
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200921 smp->data.type = SMP_T_STR;
922 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
923 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
924 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200925
926 smp->flags &= ~SMP_F_NOT_LAST;
927 return 0;
928}
929
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200930/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
931 * the right channel. So instead of duplicating the code, we just change the
932 * keyword and then fallback on smp_fetch_hdr().
933 */
934static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
935{
936 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
937 return smp_fetch_hdr(args, smp, kw, private);
938}
939
Willy Tarreau79e57332018-10-02 16:01:16 +0200940/* 6. Check on HTTP header count. The number of occurrences is returned.
941 * Accepts exactly 1 argument of type string.
942 */
943static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
944{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200945 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
946 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200947 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200948 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200949 struct http_hdr_ctx ctx;
950 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200951 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200952
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200953 if (!htx)
954 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200955
Christopher Faulet623af932021-01-29 11:22:15 +0100956 if (args->type == ARGT_STR) {
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100957 name = ist2(args->data.str.area, args->data.str.data);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200958 } else {
Tim Duesterhus68a088d2021-02-28 16:11:37 +0100959 name = IST_NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200960 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200961
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200962 ctx.blk = NULL;
963 cnt = 0;
964 while (http_find_header(htx, name, &ctx, 0))
965 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200966
967 smp->data.type = SMP_T_SINT;
968 smp->data.u.sint = cnt;
969 smp->flags = SMP_F_VOL_HDR;
970 return 1;
971}
972
973/* Fetch an HTTP header's integer value. The integer value is returned. It
974 * takes a mandatory argument of type string and an optional one of type int
975 * to designate a specific occurrence. It returns an unsigned integer, which
976 * may or may not be appropriate for everything.
977 */
978static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
979{
980 int ret = smp_fetch_hdr(args, smp, kw, private);
981
982 if (ret > 0) {
983 smp->data.type = SMP_T_SINT;
984 smp->data.u.sint = strl2ic(smp->data.u.str.area,
985 smp->data.u.str.data);
986 }
987
988 return ret;
989}
990
991/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
992 * and an optional one of type int to designate a specific occurrence.
Willy Tarreau7b0e00d2021-03-25 14:12:29 +0100993 * It returns an IPv4 or IPv6 address. Addresses surrounded by invalid chars
994 * are rejected. However IPv4 addresses may be followed with a colon and a
995 * valid port number.
Willy Tarreau79e57332018-10-02 16:01:16 +0200996 */
997static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
998{
Tim Duesterhus5cd00872020-06-26 15:44:48 +0200999 struct buffer *temp = get_trash_chunk();
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001000 int ret, len;
1001 int port;
Willy Tarreau79e57332018-10-02 16:01:16 +02001002
1003 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001004 if (smp->data.u.str.data < temp->size - 1) {
1005 memcpy(temp->area, smp->data.u.str.area,
1006 smp->data.u.str.data);
1007 temp->area[smp->data.u.str.data] = '\0';
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001008 len = url2ipv4((char *) temp->area, &smp->data.u.ipv4);
Willy Tarreau645dc082021-03-31 11:41:36 +02001009 if (len > 0 && len == smp->data.u.str.data) {
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001010 /* plain IPv4 address */
1011 smp->data.type = SMP_T_IPV4;
1012 break;
1013 } else if (len > 0 && temp->area[len] == ':' &&
1014 strl2irc(temp->area + len + 1, smp->data.u.str.data - len - 1, &port) == 0 &&
1015 port >= 0 && port <= 65535) {
1016 /* IPv4 address suffixed with ':' followed by a valid port number */
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001017 smp->data.type = SMP_T_IPV4;
1018 break;
1019 } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1020 smp->data.type = SMP_T_IPV6;
1021 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001022 }
1023 }
1024
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001025 /* if the header doesn't match an IP address, fetch next one */
1026 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001027 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001028 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001029 return ret;
1030}
Willy Tarreau79e57332018-10-02 16:01:16 +02001031
Christopher Faulete720c322020-09-02 17:25:18 +02001032/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at the
1033 * first '/' after the possible hostname. It ends before the possible '?' except
1034 * for 'pathq' keyword.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001035 */
1036static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1037{
1038 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001039 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001040 struct htx_sl *sl;
1041 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001042
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001043 if (!htx)
1044 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001045
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001046 sl = http_get_stline(htx);
Christopher Faulete720c322020-09-02 17:25:18 +02001047 path = http_get_path(htx_sl_req_uri(sl));
1048
Yves Lafonb4d37082021-02-11 11:01:28 +01001049 if (kw[4] == 'q' && (kw[0] == 'p' || kw[0] == 'b')) // pathq or baseq
Christopher Faulete720c322020-09-02 17:25:18 +02001050 path = http_get_path(htx_sl_req_uri(sl));
1051 else
1052 path = iststop(http_get_path(htx_sl_req_uri(sl)), '?');
1053
Tim Duesterhused526372020-03-05 17:56:33 +01001054 if (!isttest(path))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001055 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001056
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001057 /* OK, we got the '/' ! */
1058 smp->data.type = SMP_T_STR;
1059 smp->data.u.str.area = path.ptr;
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001060 smp->data.u.str.data = path.len;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001061 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001062 return 1;
1063}
1064
1065/* This produces a concatenation of the first occurrence of the Host header
1066 * followed by the path component if it begins with a slash ('/'). This means
1067 * that '*' will not be added, resulting in exactly the first Host entry.
1068 * If no Host header is found, then the path is returned as-is. The returned
1069 * value is stored in the trash so it does not need to be marked constant.
1070 * The returned sample is of type string.
1071 */
1072static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1073{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001074 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001075 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001076 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001077 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001078 struct http_hdr_ctx ctx;
1079 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001080
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001081 if (!htx)
1082 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001083
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001084 ctx.blk = NULL;
1085 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1086 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001087
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001088 /* OK we have the header value in ctx.value */
1089 temp = get_trash_chunk();
1090 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001091
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001092 /* now retrieve the path */
1093 sl = http_get_stline(htx);
1094 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001095 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001096 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001097
Yves Lafonb4d37082021-02-11 11:01:28 +01001098 if (kw[4] == 'q' && kw[0] == 'b') { // baseq
1099 len = path.len;
1100 } else {
1101 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1102 ;
1103 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001104
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001105 if (len && *(path.ptr) == '/')
1106 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001107 }
1108
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001109 smp->data.type = SMP_T_STR;
1110 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001111 smp->flags = SMP_F_VOL_1ST;
1112 return 1;
1113}
1114
1115/* This produces a 32-bit hash of the concatenation of the first occurrence of
1116 * the Host header followed by the path component if it begins with a slash ('/').
1117 * This means that '*' will not be added, resulting in exactly the first Host
1118 * entry. If no Host header is found, then the path is used. The resulting value
1119 * is hashed using the path hash followed by a full avalanche hash and provides a
1120 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1121 * high-traffic sites without having to store whole paths.
1122 */
1123static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1124{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001125 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001126 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001127 struct htx_sl *sl;
1128 struct http_hdr_ctx ctx;
1129 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001130 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001131
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001132 if (!htx)
1133 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001134
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001135 ctx.blk = NULL;
1136 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1137 /* OK we have the header value in ctx.value */
1138 while (ctx.value.len--)
1139 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001140 }
1141
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001142 /* now retrieve the path */
1143 sl = http_get_stline(htx);
1144 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001145 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001146 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001147
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001148 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1149 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001150
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001151 if (len && *(path.ptr) == '/') {
1152 while (len--)
1153 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001154 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001155 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001156
Willy Tarreau79e57332018-10-02 16:01:16 +02001157 hash = full_hash(hash);
1158
1159 smp->data.type = SMP_T_SINT;
1160 smp->data.u.sint = hash;
1161 smp->flags = SMP_F_VOL_1ST;
1162 return 1;
1163}
1164
1165/* This concatenates the source address with the 32-bit hash of the Host and
1166 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1167 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1168 * on the source address length. The path hash is stored before the address so
1169 * that in environments where IPv6 is insignificant, truncating the output to
1170 * 8 bytes would still work.
1171 */
1172static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1173{
1174 struct buffer *temp;
1175 struct connection *cli_conn = objt_conn(smp->sess->origin);
1176
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001177 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001178 return 0;
1179
1180 if (!smp_fetch_base32(args, smp, kw, private))
1181 return 0;
1182
1183 temp = get_trash_chunk();
1184 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1185 temp->data += sizeof(unsigned int);
1186
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001187 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001188 case AF_INET:
1189 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001190 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001191 4);
1192 temp->data += 4;
1193 break;
1194 case AF_INET6:
1195 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001196 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001197 16);
1198 temp->data += 16;
1199 break;
1200 default:
1201 return 0;
1202 }
1203
1204 smp->data.u.str = *temp;
1205 smp->data.type = SMP_T_BIN;
1206 return 1;
1207}
1208
1209/* Extracts the query string, which comes after the question mark '?'. If no
1210 * question mark is found, nothing is returned. Otherwise it returns a sample
1211 * of type string carrying the whole query string.
1212 */
1213static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1214{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001215 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001216 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001217 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001218 char *ptr, *end;
1219
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001220 if (!htx)
1221 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001222
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001223 sl = http_get_stline(htx);
1224 ptr = HTX_SL_REQ_UPTR(sl);
1225 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001226
1227 /* look up the '?' */
1228 do {
1229 if (ptr == end)
1230 return 0;
1231 } while (*ptr++ != '?');
1232
1233 smp->data.type = SMP_T_STR;
1234 smp->data.u.str.area = ptr;
1235 smp->data.u.str.data = end - ptr;
1236 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1237 return 1;
1238}
1239
1240static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1241{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001242 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001243 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001244
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001245 if (!htx)
1246 return 0;
1247 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001248 smp->data.u.sint = 1;
1249 return 1;
1250}
1251
1252/* return a valid test if the current request is the first one on the connection */
1253static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1254{
Willy Tarreau79512b62020-04-29 11:52:13 +02001255 if (!smp->strm)
1256 return 0;
1257
Willy Tarreau79e57332018-10-02 16:01:16 +02001258 smp->data.type = SMP_T_BOOL;
1259 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1260 return 1;
1261}
1262
Christopher Fauleta4063562019-08-02 11:51:37 +02001263/* Fetch the authentication method if there is an Authorization header. It
1264 * relies on get_http_auth()
1265 */
1266static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1267{
1268 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001269 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001270 struct http_txn *txn;
1271
1272 if (!htx)
1273 return 0;
1274
1275 txn = smp->strm->txn;
1276 if (!get_http_auth(smp, htx))
1277 return 0;
1278
1279 switch (txn->auth.method) {
1280 case HTTP_AUTH_BASIC:
1281 smp->data.u.str.area = "Basic";
1282 smp->data.u.str.data = 5;
1283 break;
1284 case HTTP_AUTH_DIGEST:
1285 /* Unexpected because not supported */
1286 smp->data.u.str.area = "Digest";
1287 smp->data.u.str.data = 6;
1288 break;
1289 default:
1290 return 0;
1291 }
1292
1293 smp->data.type = SMP_T_STR;
1294 smp->flags = SMP_F_CONST;
1295 return 1;
1296}
1297
1298/* Fetch the user supplied if there is an Authorization header. It relies on
1299 * get_http_auth()
1300 */
1301static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1302{
1303 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001304 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001305 struct http_txn *txn;
1306
1307 if (!htx)
1308 return 0;
1309
1310 txn = smp->strm->txn;
1311 if (!get_http_auth(smp, htx))
1312 return 0;
1313
1314 smp->data.type = SMP_T_STR;
1315 smp->data.u.str.area = txn->auth.user;
1316 smp->data.u.str.data = strlen(txn->auth.user);
1317 smp->flags = SMP_F_CONST;
1318 return 1;
1319}
1320
1321/* Fetch the password supplied if there is an Authorization header. It relies on
1322 * get_http_auth()
1323 */
1324static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1325{
1326 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001327 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001328 struct http_txn *txn;
1329
1330 if (!htx)
1331 return 0;
1332
1333 txn = smp->strm->txn;
1334 if (!get_http_auth(smp, htx))
1335 return 0;
1336
1337 smp->data.type = SMP_T_STR;
1338 smp->data.u.str.area = txn->auth.pass;
1339 smp->data.u.str.data = strlen(txn->auth.pass);
1340 smp->flags = SMP_F_CONST;
1341 return 1;
1342}
1343
Willy Tarreau79e57332018-10-02 16:01:16 +02001344/* Accepts exactly 1 argument of type userlist */
1345static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1346{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001347 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001348 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001349
Christopher Faulet623af932021-01-29 11:22:15 +01001350 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001351 return 0;
1352
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001353 if (!htx)
1354 return 0;
1355 if (!get_http_auth(smp, htx))
1356 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001357
1358 smp->data.type = SMP_T_BOOL;
1359 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001360 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001361 return 1;
1362}
1363
1364/* Accepts exactly 1 argument of type userlist */
1365static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1366{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001367 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001368 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001369
Christopher Faulet623af932021-01-29 11:22:15 +01001370 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001371 return 0;
1372
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001373 if (!htx)
1374 return 0;
1375 if (!get_http_auth(smp, htx))
1376 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001377
Willy Tarreau79e57332018-10-02 16:01:16 +02001378 /* if the user does not belong to the userlist or has a wrong password,
1379 * report that it unconditionally does not match. Otherwise we return
1380 * a string containing the username.
1381 */
1382 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1383 smp->strm->txn->auth.pass))
1384 return 0;
1385
1386 /* pat_match_auth() will need the user list */
1387 smp->ctx.a[0] = args->data.usr;
1388
1389 smp->data.type = SMP_T_STR;
1390 smp->flags = SMP_F_CONST;
1391 smp->data.u.str.area = smp->strm->txn->auth.user;
1392 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1393
1394 return 1;
1395}
1396
1397/* Fetch a captured HTTP request header. The index is the position of
1398 * the "capture" option in the configuration file
1399 */
1400static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1401{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001402 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001403 int idx;
1404
Christopher Faulet623af932021-01-29 11:22:15 +01001405 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001406 return 0;
1407
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001408 if (!smp->strm)
1409 return 0;
1410
1411 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001412 idx = args->data.sint;
1413
1414 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1415 return 0;
1416
1417 smp->data.type = SMP_T_STR;
1418 smp->flags |= SMP_F_CONST;
1419 smp->data.u.str.area = smp->strm->req_cap[idx];
1420 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1421
1422 return 1;
1423}
1424
1425/* Fetch a captured HTTP response header. The index is the position of
1426 * the "capture" option in the configuration file
1427 */
1428static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1429{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001430 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001431 int idx;
1432
Christopher Faulet623af932021-01-29 11:22:15 +01001433 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001434 return 0;
1435
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001436 if (!smp->strm)
1437 return 0;
1438
1439 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001440 idx = args->data.sint;
1441
1442 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1443 return 0;
1444
1445 smp->data.type = SMP_T_STR;
1446 smp->flags |= SMP_F_CONST;
1447 smp->data.u.str.area = smp->strm->res_cap[idx];
1448 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1449
1450 return 1;
1451}
1452
1453/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1454static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1455{
1456 struct buffer *temp;
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001457 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001458 char *ptr;
1459
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001460 if (!smp->strm)
1461 return 0;
1462
1463 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001464 if (!txn || !txn->uri)
1465 return 0;
1466
1467 ptr = txn->uri;
1468
1469 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1470 ptr++;
1471
1472 temp = get_trash_chunk();
1473 temp->area = txn->uri;
1474 temp->data = ptr - txn->uri;
1475 smp->data.u.str = *temp;
1476 smp->data.type = SMP_T_STR;
1477 smp->flags = SMP_F_CONST;
1478
1479 return 1;
1480
1481}
1482
1483/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1484static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1485{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001486 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001487 struct ist path;
1488 const char *ptr;
1489
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001490 if (!smp->strm)
1491 return 0;
1492
1493 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001494 if (!txn || !txn->uri)
1495 return 0;
1496
1497 ptr = txn->uri;
1498
1499 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1500 ptr++;
1501
1502 if (!*ptr)
1503 return 0;
1504
Christopher Faulet78337bb2018-11-15 14:35:18 +01001505 /* skip the first space and find space after URI */
1506 path = ist2(++ptr, 0);
1507 while (*ptr != ' ' && *ptr != '\0')
1508 ptr++;
1509 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001510
Christopher Faulet78337bb2018-11-15 14:35:18 +01001511 path = http_get_path(path);
Tim Duesterhused526372020-03-05 17:56:33 +01001512 if (!isttest(path))
Willy Tarreau79e57332018-10-02 16:01:16 +02001513 return 0;
1514
1515 smp->data.u.str.area = path.ptr;
1516 smp->data.u.str.data = path.len;
1517 smp->data.type = SMP_T_STR;
1518 smp->flags = SMP_F_CONST;
1519
1520 return 1;
1521}
1522
1523/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1524 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1525 */
1526static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1527{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001528 struct http_txn *txn;
1529
1530 if (!smp->strm)
1531 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001532
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001533 txn = smp->strm->txn;
Christopher Faulet09f88362021-04-01 16:00:29 +02001534 if (!txn || txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001535 return 0;
1536
1537 if (txn->req.flags & HTTP_MSGF_VER_11)
1538 smp->data.u.str.area = "HTTP/1.1";
1539 else
1540 smp->data.u.str.area = "HTTP/1.0";
1541
1542 smp->data.u.str.data = 8;
1543 smp->data.type = SMP_T_STR;
1544 smp->flags = SMP_F_CONST;
1545 return 1;
1546
1547}
1548
1549/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1550 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1551 */
1552static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1553{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001554 struct http_txn *txn;
1555
1556 if (!smp->strm)
1557 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001558
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001559 txn = smp->strm->txn;
Christopher Faulet09f88362021-04-01 16:00:29 +02001560 if (!txn || txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001561 return 0;
1562
1563 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1564 smp->data.u.str.area = "HTTP/1.1";
1565 else
1566 smp->data.u.str.area = "HTTP/1.0";
1567
1568 smp->data.u.str.data = 8;
1569 smp->data.type = SMP_T_STR;
1570 smp->flags = SMP_F_CONST;
1571 return 1;
1572
1573}
1574
1575/* Iterate over all cookies present in a message. The context is stored in
1576 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1577 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1578 * the direction, multiple cookies may be parsed on the same line or not.
Maciej Zdebdea7c202020-11-13 09:38:06 +00001579 * If provided, the searched cookie name is in args, in args->data.str. If
1580 * the input options indicate that no iterating is desired, then only last
1581 * value is fetched if any. If no cookie name is provided, the first cookie
1582 * value found is fetched. The returned sample is of type CSTR. Can be used
1583 * to parse cookies in other files.
Willy Tarreau79e57332018-10-02 16:01:16 +02001584 */
1585static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1586{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001587 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1588 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001589 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001590 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001591 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1592 struct ist hdr;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001593 char *cook = NULL;
1594 size_t cook_l = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001595 int found = 0;
1596
Christopher Faulet623af932021-01-29 11:22:15 +01001597 if (args->type == ARGT_STR) {
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001598 cook = args->data.str.area;
1599 cook_l = args->data.str.data;
1600 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001601
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001602 if (!ctx) {
1603 /* first call */
1604 ctx = &static_http_hdr_ctx;
1605 ctx->blk = NULL;
1606 smp->ctx.a[2] = ctx;
1607 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001608
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001609 if (!htx)
1610 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001611
Christopher Faulet16032ab2020-04-30 11:30:00 +02001612 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001613
Maciej Zdebdea7c202020-11-13 09:38:06 +00001614 /* OK so basically here, either we want only one value or we want to
1615 * iterate over all of them and we fetch the next one. In this last case
1616 * SMP_OPT_ITERATE option is set.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001617 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001618
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001619 if (!(smp->flags & SMP_F_NOT_LAST)) {
1620 /* search for the header from the beginning, we must first initialize
1621 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001622 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001623 smp->ctx.a[0] = NULL;
1624 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001625 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001626
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001627 smp->flags |= SMP_F_VOL_HDR;
1628 while (1) {
1629 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1630 if (!smp->ctx.a[0]) {
1631 if (!http_find_header(htx, hdr, ctx, 0))
1632 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001633
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001634 if (ctx->value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001635 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001636
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001637 smp->ctx.a[0] = ctx->value.ptr;
1638 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001639 }
1640
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001641 smp->data.type = SMP_T_STR;
1642 smp->flags |= SMP_F_CONST;
1643 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001644 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001645 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1646 &smp->data.u.str.area,
1647 &smp->data.u.str.data);
1648 if (smp->ctx.a[0]) {
1649 found = 1;
Maciej Zdebdea7c202020-11-13 09:38:06 +00001650 if (smp->opt & SMP_OPT_ITERATE) {
1651 /* iterate on cookie value */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001652 smp->flags |= SMP_F_NOT_LAST;
1653 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001654 }
Maciej Zdebdea7c202020-11-13 09:38:06 +00001655 if (args->data.str.data == 0) {
1656 /* No cookie name, first occurrence returned */
1657 break;
1658 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001659 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001660 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001661 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001662
Willy Tarreau79e57332018-10-02 16:01:16 +02001663 /* all cookie headers and values were scanned. If we're looking for the
1664 * last occurrence, we may return it now.
1665 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001666 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001667 smp->flags &= ~SMP_F_NOT_LAST;
1668 return found;
1669}
1670
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001671/* Same than smp_fetch_cookie() but only relies on the sample direction to
1672 * choose the right channel. So instead of duplicating the code, we just change
1673 * the keyword and then fallback on smp_fetch_cookie().
1674 */
1675static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1676{
1677 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1678 return smp_fetch_cookie(args, smp, kw, private);
1679}
1680
Willy Tarreau79e57332018-10-02 16:01:16 +02001681/* Iterate over all cookies present in a request to count how many occurrences
1682 * match the name in args and args->data.str.len. If <multi> is non-null, then
1683 * multiple cookies may be parsed on the same line. The returned sample is of
1684 * type UINT. Accepts exactly 1 argument of type string.
1685 */
1686static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1687{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001688 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1689 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001690 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001691 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001692 struct http_hdr_ctx ctx;
1693 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001694 char *val_beg, *val_end;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001695 char *cook = NULL;
1696 size_t cook_l = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001697 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001698
Christopher Faulet623af932021-01-29 11:22:15 +01001699 if (args->type == ARGT_STR){
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001700 cook = args->data.str.area;
1701 cook_l = args->data.str.data;
1702 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001703
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001704 if (!htx)
1705 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001706
Christopher Faulet16032ab2020-04-30 11:30:00 +02001707 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001708
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001709 val_end = val_beg = NULL;
1710 ctx.blk = NULL;
1711 cnt = 0;
1712 while (1) {
1713 /* Note: val_beg == NULL every time we need to fetch a new header */
1714 if (!val_beg) {
1715 if (!http_find_header(htx, hdr, &ctx, 0))
1716 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001717
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001718 if (ctx.value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001719 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001720
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001721 val_beg = ctx.value.ptr;
1722 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001723 }
1724
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001725 smp->data.type = SMP_T_STR;
1726 smp->flags |= SMP_F_CONST;
1727 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001728 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001729 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1730 &smp->data.u.str.area,
1731 &smp->data.u.str.data))) {
1732 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001733 }
1734 }
1735
1736 smp->data.type = SMP_T_SINT;
1737 smp->data.u.sint = cnt;
1738 smp->flags |= SMP_F_VOL_HDR;
1739 return 1;
1740}
1741
1742/* Fetch an cookie's integer value. The integer value is returned. It
1743 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1744 */
1745static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1746{
1747 int ret = smp_fetch_cookie(args, smp, kw, private);
1748
1749 if (ret > 0) {
1750 smp->data.type = SMP_T_SINT;
1751 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1752 smp->data.u.str.data);
1753 }
1754
1755 return ret;
1756}
1757
1758/************************************************************************/
1759/* The code below is dedicated to sample fetches */
1760/************************************************************************/
1761
1762/* This scans a URL-encoded query string. It takes an optionally wrapping
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001763 * string whose first contiguous chunk has its beginning in ctx->a[0] and end
Willy Tarreau79e57332018-10-02 16:01:16 +02001764 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1765 * pointers are updated for next iteration before leaving.
1766 */
1767static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1768{
1769 const char *vstart, *vend;
1770 struct buffer *temp;
1771 const char **chunks = (const char **)smp->ctx.a;
1772
1773 if (!http_find_next_url_param(chunks, name, name_len,
1774 &vstart, &vend, delim))
1775 return 0;
1776
1777 /* Create sample. If the value is contiguous, return the pointer as CONST,
1778 * if the value is wrapped, copy-it in a buffer.
1779 */
1780 smp->data.type = SMP_T_STR;
1781 if (chunks[2] &&
1782 vstart >= chunks[0] && vstart <= chunks[1] &&
1783 vend >= chunks[2] && vend <= chunks[3]) {
1784 /* Wrapped case. */
1785 temp = get_trash_chunk();
1786 memcpy(temp->area, vstart, chunks[1] - vstart);
1787 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1788 vend - chunks[2]);
1789 smp->data.u.str.area = temp->area;
1790 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1791 } else {
1792 /* Contiguous case. */
1793 smp->data.u.str.area = (char *)vstart;
1794 smp->data.u.str.data = vend - vstart;
1795 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1796 }
1797
1798 /* Update context, check wrapping. */
1799 chunks[0] = vend;
1800 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1801 chunks[1] = chunks[3];
1802 chunks[2] = NULL;
1803 }
1804
1805 if (chunks[0] < chunks[1])
1806 smp->flags |= SMP_F_NOT_LAST;
1807
1808 return 1;
1809}
1810
1811/* This function iterates over each parameter of the query string. It uses
1812 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1813 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1814 * An optional parameter name is passed in args[0], otherwise any parameter is
1815 * considered. It supports an optional delimiter argument for the beginning of
1816 * the string in args[1], which defaults to "?".
1817 */
1818static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1819{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001820 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001821 char delim = '?';
1822 const char *name;
1823 int name_len;
1824
Christopher Faulet623af932021-01-29 11:22:15 +01001825 if ((args[0].type && args[0].type != ARGT_STR) ||
Willy Tarreau79e57332018-10-02 16:01:16 +02001826 (args[1].type && args[1].type != ARGT_STR))
1827 return 0;
1828
1829 name = "";
1830 name_len = 0;
1831 if (args->type == ARGT_STR) {
1832 name = args->data.str.area;
1833 name_len = args->data.str.data;
1834 }
1835
1836 if (args[1].type)
1837 delim = *args[1].data.str.area;
1838
1839 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001840 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001841 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001842
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001843 if (!htx)
1844 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001845
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001846 sl = http_get_stline(htx);
1847 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1848 if (!smp->ctx.a[0])
1849 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001850
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001851 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001852
1853 /* Assume that the context is filled with NULL pointer
1854 * before the first call.
1855 * smp->ctx.a[2] = NULL;
1856 * smp->ctx.a[3] = NULL;
1857 */
1858 }
1859
1860 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1861}
1862
1863/* This function iterates over each parameter of the body. This requires
1864 * that the body has been waited for using http-buffer-request. It uses
1865 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001866 * contiguous part of the body, and optionally ctx->a[2..3] to reference the
Willy Tarreau79e57332018-10-02 16:01:16 +02001867 * optional second part if the body wraps at the end of the buffer. An optional
1868 * parameter name is passed in args[0], otherwise any parameter is considered.
1869 */
1870static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1871{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001872 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001873 const char *name;
1874 int name_len;
1875
Christopher Faulet623af932021-01-29 11:22:15 +01001876 if (args[0].type && args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001877 return 0;
1878
1879 name = "";
1880 name_len = 0;
1881 if (args[0].type == ARGT_STR) {
1882 name = args[0].data.str.area;
1883 name_len = args[0].data.str.data;
1884 }
1885
1886 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulete596d182020-05-05 17:46:34 +02001887 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001888 struct buffer *temp;
1889 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001890
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001891 if (!htx)
1892 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001893
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001894 temp = get_trash_chunk();
1895 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1896 struct htx_blk *blk = htx_get_blk(htx, pos);
1897 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001898
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001899 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001900 break;
1901 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001902 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001903 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001904 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001905 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001906
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001907 smp->ctx.a[0] = temp->area;
1908 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001909
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001910 /* Assume that the context is filled with NULL pointer
1911 * before the first call.
1912 * smp->ctx.a[2] = NULL;
1913 * smp->ctx.a[3] = NULL;
1914 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001915
Willy Tarreau79e57332018-10-02 16:01:16 +02001916 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001917
Willy Tarreau79e57332018-10-02 16:01:16 +02001918 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1919}
1920
1921/* Return the signed integer value for the specified url parameter (see url_param
1922 * above).
1923 */
1924static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1925{
1926 int ret = smp_fetch_url_param(args, smp, kw, private);
1927
1928 if (ret > 0) {
1929 smp->data.type = SMP_T_SINT;
1930 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1931 smp->data.u.str.data);
1932 }
1933
1934 return ret;
1935}
1936
1937/* This produces a 32-bit hash of the concatenation of the first occurrence of
1938 * the Host header followed by the path component if it begins with a slash ('/').
1939 * This means that '*' will not be added, resulting in exactly the first Host
1940 * entry. If no Host header is found, then the path is used. The resulting value
1941 * is hashed using the url hash followed by a full avalanche hash and provides a
1942 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1943 * high-traffic sites without having to store whole paths.
1944 * this differs from the base32 functions in that it includes the url parameters
1945 * as well as the path
1946 */
1947static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1948{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001949 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001950 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001951 struct http_hdr_ctx ctx;
1952 struct htx_sl *sl;
1953 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001954 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001955
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001956 if (!htx)
1957 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001958
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001959 ctx.blk = NULL;
1960 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1961 /* OK we have the header value in ctx.value */
1962 while (ctx.value.len--)
1963 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001964 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001965
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001966 /* now retrieve the path */
1967 sl = http_get_stline(htx);
1968 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001969 if (path.len && *(path.ptr) == '/') {
1970 while (path.len--)
1971 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001972 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001973
Willy Tarreau79e57332018-10-02 16:01:16 +02001974 hash = full_hash(hash);
1975
1976 smp->data.type = SMP_T_SINT;
1977 smp->data.u.sint = hash;
1978 smp->flags = SMP_F_VOL_1ST;
1979 return 1;
1980}
1981
1982/* This concatenates the source address with the 32-bit hash of the Host and
1983 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1984 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1985 * on the source address length. The URL hash is stored before the address so
1986 * that in environments where IPv6 is insignificant, truncating the output to
1987 * 8 bytes would still work.
1988 */
1989static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1990{
1991 struct buffer *temp;
1992 struct connection *cli_conn = objt_conn(smp->sess->origin);
1993
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001994 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001995 return 0;
1996
1997 if (!smp_fetch_url32(args, smp, kw, private))
1998 return 0;
1999
2000 temp = get_trash_chunk();
2001 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
2002 temp->data += sizeof(unsigned int);
2003
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002004 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02002005 case AF_INET:
2006 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002007 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002008 4);
2009 temp->data += 4;
2010 break;
2011 case AF_INET6:
2012 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002013 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002014 16);
2015 temp->data += 16;
2016 break;
2017 default:
2018 return 0;
2019 }
2020
2021 smp->data.u.str = *temp;
2022 smp->data.type = SMP_T_BIN;
2023 return 1;
2024}
2025
2026/************************************************************************/
2027/* Other utility functions */
2028/************************************************************************/
2029
2030/* This function is used to validate the arguments passed to any "hdr" fetch
2031 * keyword. These keywords support an optional positive or negative occurrence
2032 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2033 * is assumed that the types are already the correct ones. Returns 0 on error,
2034 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2035 * error message in case of error, that the caller is responsible for freeing.
2036 * The initial location must either be freeable or NULL.
2037 * Note: this function's pointer is checked from Lua.
2038 */
2039int val_hdr(struct arg *arg, char **err_msg)
2040{
2041 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2042 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2043 return 0;
2044 }
2045 return 1;
2046}
2047
2048/************************************************************************/
2049/* All supported sample fetch keywords must be declared here. */
2050/************************************************************************/
2051
2052/* Note: must not be declared <const> as its list will be overwritten */
2053static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2054 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2055 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2056 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Yves Lafonb4d37082021-02-11 11:01:28 +01002057 { "baseq", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002058
2059 /* capture are allocated and are permanent in the stream */
2060 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2061
2062 /* retrieve these captures from the HTTP logs */
2063 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2064 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2065 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2066
2067 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2068 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2069
2070 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2071 * are only here to match the ACL's name, are request-only and are used
2072 * for ACL compatibility only.
2073 */
2074 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002075 { "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 +02002076 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2077 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2078
2079 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2080 * only here to match the ACL's name, are request-only and are used for
2081 * ACL compatibility only.
2082 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002083 { "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 +02002084 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2085 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2086 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2087
Christopher Fauleta4063562019-08-02 11:51:37 +02002088 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2089 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2090 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002091 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2092 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2093 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2094 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2095 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Faulete720c322020-09-02 17:25:18 +02002096 { "pathq", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002097 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2098
2099 /* HTTP protocol on the request path */
2100 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2101 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2102
2103 /* HTTP version on the request path */
2104 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2105 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2106
2107 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2108 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2109 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2110 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2111
2112 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2113 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2114
2115 /* HTTP version on the response path */
2116 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2117 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2118
Christopher Faulete596d182020-05-05 17:46:34 +02002119 { "res.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2120 { "res.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2121 { "res.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2122
2123 { "res.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2124 { "res.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2125
Willy Tarreau79e57332018-10-02 16:01:16 +02002126 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2127 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2128 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2129 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2130
2131 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2132 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2133 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2134 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2135 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2136 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2137 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2138
2139 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2140 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2141 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2142 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2143
2144 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2145 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2146 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2147 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2148 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2149 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2150 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2151
2152 /* scook is valid only on the response and is used for ACL compatibility */
2153 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2154 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2155 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002156
2157 /* shdr is valid only on the response and is used for ACL compatibility */
2158 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2159 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2160 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2161 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2162
2163 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2164 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2165 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2166 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2167 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2168 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2169 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2170 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2171 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2172 { "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 +02002173
Willy Tarreau79e57332018-10-02 16:01:16 +02002174 { /* END */ },
2175}};
2176
Willy Tarreau0108d902018-11-25 19:14:37 +01002177INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002178
2179/*
2180 * Local variables:
2181 * c-indent-level: 8
2182 * c-basic-offset: 8
2183 * End:
2184 */