blob: 0d2be7ea40df755021d851a5f4f3edbfd5f9417f [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
345 htx = smp_prefetch_htx(smp, chn, NULL, 0);
346 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
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200708 if (!htx)
709 return 0;
710 sl = http_get_stline(htx);
711 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +0200712
Willy Tarreau79e57332018-10-02 16:01:16 +0200713 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
714 return 0;
715
716 smp->data.type = SMP_T_IPV4;
717 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
718 smp->flags = 0;
719 return 1;
720}
721
722static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
723{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200724 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200725 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200726 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200727 struct sockaddr_storage addr;
728
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200729 if (!htx)
730 return 0;
731 sl = http_get_stline(htx);
732 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200733
Willy Tarreau79e57332018-10-02 16:01:16 +0200734 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
735 return 0;
736
737 smp->data.type = SMP_T_SINT;
738 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
739 smp->flags = 0;
740 return 1;
741}
742
743/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
744 * Accepts an optional argument of type string containing the header field name,
745 * and an optional argument of type signed or unsigned integer to request an
746 * explicit occurrence of the header. Note that in the event of a missing name,
747 * headers are considered from the first one. It does not stop on commas and
748 * returns full lines instead (useful for User-Agent or Date for example).
749 */
750static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
751{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200752 /* possible keywords: req.fhdr, res.fhdr */
753 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200754 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200755 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200756 struct http_hdr_ctx *ctx = smp->ctx.a[0];
757 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200758 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200759
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200760 if (!ctx) {
761 /* first call */
762 ctx = &static_http_hdr_ctx;
763 ctx->blk = NULL;
764 smp->ctx.a[0] = ctx;
765 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200766
Christopher Faulet623af932021-01-29 11:22:15 +0100767 if (args[0].type != ARGT_STR)
768 return 0;
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100769 name = ist2(args[0].data.str.area, args[0].data.str.data);
Willy Tarreau79e57332018-10-02 16:01:16 +0200770
Christopher Faulet623af932021-01-29 11:22:15 +0100771 if (args[1].type == ARGT_SINT)
772 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200773
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200774 if (!htx)
775 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200776
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200777 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
778 /* search for header from the beginning */
779 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200780
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200781 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
782 /* no explicit occurrence and single fetch => last header by default */
783 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200784
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200785 if (!occ)
786 /* prepare to report multiple occurrences for ACL fetches */
787 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200788
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200789 smp->data.type = SMP_T_STR;
790 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
791 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
792 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200793 smp->flags &= ~SMP_F_NOT_LAST;
794 return 0;
795}
796
797/* 6. Check on HTTP header count. The number of occurrences is returned.
798 * Accepts exactly 1 argument of type string. It does not stop on commas and
799 * returns full lines instead (useful for User-Agent or Date for example).
800 */
801static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
802{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200803 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
804 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200805 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200806 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200807 struct http_hdr_ctx ctx;
808 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200809 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200810
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200811 if (!htx)
812 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200813
Christopher Faulet623af932021-01-29 11:22:15 +0100814 if (args->type == ARGT_STR) {
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100815 name = ist2(args->data.str.area, args->data.str.data);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200816 } else {
Tim Duesterhus68a088d2021-02-28 16:11:37 +0100817 name = IST_NULL;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200818 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200819
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200820 ctx.blk = NULL;
821 cnt = 0;
822 while (http_find_header(htx, name, &ctx, 1))
823 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200824 smp->data.type = SMP_T_SINT;
825 smp->data.u.sint = cnt;
826 smp->flags = SMP_F_VOL_HDR;
827 return 1;
828}
829
830static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
831{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200832 /* possible keywords: req.hdr_names, res.hdr_names */
833 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200834 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200835 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200836 struct buffer *temp;
837 char del = ',';
838
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200839 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200840
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200841 if (!htx)
842 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200843
Christopher Faulet623af932021-01-29 11:22:15 +0100844 if (args->type == ARGT_STR)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200845 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200846
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200847 temp = get_trash_chunk();
848 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
849 struct htx_blk *blk = htx_get_blk(htx, pos);
850 enum htx_blk_type type = htx_get_blk_type(blk);
851 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200852
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200853 if (type == HTX_BLK_EOH)
854 break;
855 if (type != HTX_BLK_HDR)
856 continue;
857 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200858
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200859 if (temp->data)
860 temp->area[temp->data++] = del;
861 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200862 }
863
864 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200865 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200866 smp->flags = SMP_F_VOL_HDR;
867 return 1;
868}
869
870/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
871 * Accepts an optional argument of type string containing the header field name,
872 * and an optional argument of type signed or unsigned integer to request an
873 * explicit occurrence of the header. Note that in the event of a missing name,
874 * headers are considered from the first one.
875 */
876static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
877{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200878 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
879 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200880 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200881 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200882 struct http_hdr_ctx *ctx = smp->ctx.a[0];
883 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200884 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200885
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200886 if (!ctx) {
887 /* first call */
888 ctx = &static_http_hdr_ctx;
889 ctx->blk = NULL;
890 smp->ctx.a[0] = ctx;
891 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200892
Christopher Faulet623af932021-01-29 11:22:15 +0100893 if (args[0].type != ARGT_STR)
894 return 0;
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100895 name = ist2(args[0].data.str.area, args[0].data.str.data);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200896
Christopher Faulet623af932021-01-29 11:22:15 +0100897 if (args[1].type == ARGT_SINT)
898 occ = args[1].data.sint;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200899
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200900 if (!htx)
901 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200902
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200903 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
904 /* search for header from the beginning */
905 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200906
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200907 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
908 /* no explicit occurrence and single fetch => last header by default */
909 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200910
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200911 if (!occ)
912 /* prepare to report multiple occurrences for ACL fetches */
913 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200914
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200915 smp->data.type = SMP_T_STR;
916 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
917 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
918 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200919
920 smp->flags &= ~SMP_F_NOT_LAST;
921 return 0;
922}
923
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200924/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
925 * the right channel. So instead of duplicating the code, we just change the
926 * keyword and then fallback on smp_fetch_hdr().
927 */
928static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
929{
930 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
931 return smp_fetch_hdr(args, smp, kw, private);
932}
933
Willy Tarreau79e57332018-10-02 16:01:16 +0200934/* 6. Check on HTTP header count. The number of occurrences is returned.
935 * Accepts exactly 1 argument of type string.
936 */
937static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
938{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200939 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
940 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200941 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200942 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200943 struct http_hdr_ctx ctx;
944 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200945 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200946
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200947 if (!htx)
948 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200949
Christopher Faulet623af932021-01-29 11:22:15 +0100950 if (args->type == ARGT_STR) {
Tim Duesterhus92c696e2021-02-28 16:11:36 +0100951 name = ist2(args->data.str.area, args->data.str.data);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200952 } else {
Tim Duesterhus68a088d2021-02-28 16:11:37 +0100953 name = IST_NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200954 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200955
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200956 ctx.blk = NULL;
957 cnt = 0;
958 while (http_find_header(htx, name, &ctx, 0))
959 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200960
961 smp->data.type = SMP_T_SINT;
962 smp->data.u.sint = cnt;
963 smp->flags = SMP_F_VOL_HDR;
964 return 1;
965}
966
967/* Fetch an HTTP header's integer value. The integer value is returned. It
968 * takes a mandatory argument of type string and an optional one of type int
969 * to designate a specific occurrence. It returns an unsigned integer, which
970 * may or may not be appropriate for everything.
971 */
972static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
973{
974 int ret = smp_fetch_hdr(args, smp, kw, private);
975
976 if (ret > 0) {
977 smp->data.type = SMP_T_SINT;
978 smp->data.u.sint = strl2ic(smp->data.u.str.area,
979 smp->data.u.str.data);
980 }
981
982 return ret;
983}
984
985/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
986 * and an optional one of type int to designate a specific occurrence.
Willy Tarreau7b0e00d2021-03-25 14:12:29 +0100987 * It returns an IPv4 or IPv6 address. Addresses surrounded by invalid chars
988 * are rejected. However IPv4 addresses may be followed with a colon and a
989 * valid port number.
Willy Tarreau79e57332018-10-02 16:01:16 +0200990 */
991static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
992{
Tim Duesterhus5cd00872020-06-26 15:44:48 +0200993 struct buffer *temp = get_trash_chunk();
Willy Tarreau7b0e00d2021-03-25 14:12:29 +0100994 int ret, len;
995 int port;
Willy Tarreau79e57332018-10-02 16:01:16 +0200996
997 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Tim Duesterhus5cd00872020-06-26 15:44:48 +0200998 if (smp->data.u.str.data < temp->size - 1) {
999 memcpy(temp->area, smp->data.u.str.area,
1000 smp->data.u.str.data);
1001 temp->area[smp->data.u.str.data] = '\0';
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001002 len = url2ipv4((char *) temp->area, &smp->data.u.ipv4);
Willy Tarreau645dc082021-03-31 11:41:36 +02001003 if (len > 0 && len == smp->data.u.str.data) {
Willy Tarreau7b0e00d2021-03-25 14:12:29 +01001004 /* plain IPv4 address */
1005 smp->data.type = SMP_T_IPV4;
1006 break;
1007 } else if (len > 0 && temp->area[len] == ':' &&
1008 strl2irc(temp->area + len + 1, smp->data.u.str.data - len - 1, &port) == 0 &&
1009 port >= 0 && port <= 65535) {
1010 /* IPv4 address suffixed with ':' followed by a valid port number */
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001011 smp->data.type = SMP_T_IPV4;
1012 break;
1013 } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1014 smp->data.type = SMP_T_IPV6;
1015 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001016 }
1017 }
1018
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001019 /* if the header doesn't match an IP address, fetch next one */
1020 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001021 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001022 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001023 return ret;
1024}
Willy Tarreau79e57332018-10-02 16:01:16 +02001025
Christopher Faulete720c322020-09-02 17:25:18 +02001026/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at the
1027 * first '/' after the possible hostname. It ends before the possible '?' except
1028 * for 'pathq' keyword.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001029 */
1030static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1031{
1032 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001033 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001034 struct htx_sl *sl;
1035 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001036
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001037 if (!htx)
1038 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001039
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001040 sl = http_get_stline(htx);
Christopher Faulete720c322020-09-02 17:25:18 +02001041 path = http_get_path(htx_sl_req_uri(sl));
1042
Yves Lafonb4d37082021-02-11 11:01:28 +01001043 if (kw[4] == 'q' && (kw[0] == 'p' || kw[0] == 'b')) // pathq or baseq
Christopher Faulete720c322020-09-02 17:25:18 +02001044 path = http_get_path(htx_sl_req_uri(sl));
1045 else
1046 path = iststop(http_get_path(htx_sl_req_uri(sl)), '?');
1047
Tim Duesterhused526372020-03-05 17:56:33 +01001048 if (!isttest(path))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001049 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001050
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001051 /* OK, we got the '/' ! */
1052 smp->data.type = SMP_T_STR;
1053 smp->data.u.str.area = path.ptr;
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001054 smp->data.u.str.data = path.len;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001055 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001056 return 1;
1057}
1058
1059/* This produces a concatenation of the first occurrence of the Host header
1060 * followed by the path component if it begins with a slash ('/'). This means
1061 * that '*' will not be added, resulting in exactly the first Host entry.
1062 * If no Host header is found, then the path is returned as-is. The returned
1063 * value is stored in the trash so it does not need to be marked constant.
1064 * The returned sample is of type string.
1065 */
1066static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1067{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001068 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001069 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001070 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001071 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001072 struct http_hdr_ctx ctx;
1073 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001074
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001075 if (!htx)
1076 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001077
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001078 ctx.blk = NULL;
1079 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1080 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001081
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001082 /* OK we have the header value in ctx.value */
1083 temp = get_trash_chunk();
1084 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001085
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001086 /* now retrieve the path */
1087 sl = http_get_stline(htx);
1088 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001089 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001090 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001091
Yves Lafonb4d37082021-02-11 11:01:28 +01001092 if (kw[4] == 'q' && kw[0] == 'b') { // baseq
1093 len = path.len;
1094 } else {
1095 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1096 ;
1097 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001098
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001099 if (len && *(path.ptr) == '/')
1100 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001101 }
1102
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001103 smp->data.type = SMP_T_STR;
1104 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001105 smp->flags = SMP_F_VOL_1ST;
1106 return 1;
1107}
1108
1109/* This produces a 32-bit hash of the concatenation of the first occurrence of
1110 * the Host header followed by the path component if it begins with a slash ('/').
1111 * This means that '*' will not be added, resulting in exactly the first Host
1112 * entry. If no Host header is found, then the path is used. The resulting value
1113 * is hashed using the path hash followed by a full avalanche hash and provides a
1114 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1115 * high-traffic sites without having to store whole paths.
1116 */
1117static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1118{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001119 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001120 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001121 struct htx_sl *sl;
1122 struct http_hdr_ctx ctx;
1123 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001124 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001125
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001126 if (!htx)
1127 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001128
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001129 ctx.blk = NULL;
1130 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1131 /* OK we have the header value in ctx.value */
1132 while (ctx.value.len--)
1133 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001134 }
1135
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001136 /* now retrieve the path */
1137 sl = http_get_stline(htx);
1138 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001139 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001140 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001141
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001142 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1143 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001144
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001145 if (len && *(path.ptr) == '/') {
1146 while (len--)
1147 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001148 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001149 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001150
Willy Tarreau79e57332018-10-02 16:01:16 +02001151 hash = full_hash(hash);
1152
1153 smp->data.type = SMP_T_SINT;
1154 smp->data.u.sint = hash;
1155 smp->flags = SMP_F_VOL_1ST;
1156 return 1;
1157}
1158
1159/* This concatenates the source address with the 32-bit hash of the Host and
1160 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1161 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1162 * on the source address length. The path hash is stored before the address so
1163 * that in environments where IPv6 is insignificant, truncating the output to
1164 * 8 bytes would still work.
1165 */
1166static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1167{
1168 struct buffer *temp;
1169 struct connection *cli_conn = objt_conn(smp->sess->origin);
1170
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001171 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001172 return 0;
1173
1174 if (!smp_fetch_base32(args, smp, kw, private))
1175 return 0;
1176
1177 temp = get_trash_chunk();
1178 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1179 temp->data += sizeof(unsigned int);
1180
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001181 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001182 case AF_INET:
1183 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001184 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001185 4);
1186 temp->data += 4;
1187 break;
1188 case AF_INET6:
1189 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001190 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001191 16);
1192 temp->data += 16;
1193 break;
1194 default:
1195 return 0;
1196 }
1197
1198 smp->data.u.str = *temp;
1199 smp->data.type = SMP_T_BIN;
1200 return 1;
1201}
1202
1203/* Extracts the query string, which comes after the question mark '?'. If no
1204 * question mark is found, nothing is returned. Otherwise it returns a sample
1205 * of type string carrying the whole query string.
1206 */
1207static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1208{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001209 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001210 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001211 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001212 char *ptr, *end;
1213
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001214 if (!htx)
1215 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001216
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001217 sl = http_get_stline(htx);
1218 ptr = HTX_SL_REQ_UPTR(sl);
1219 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001220
1221 /* look up the '?' */
1222 do {
1223 if (ptr == end)
1224 return 0;
1225 } while (*ptr++ != '?');
1226
1227 smp->data.type = SMP_T_STR;
1228 smp->data.u.str.area = ptr;
1229 smp->data.u.str.data = end - ptr;
1230 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1231 return 1;
1232}
1233
1234static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1235{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001236 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001237 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001238
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001239 if (!htx)
1240 return 0;
1241 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001242 smp->data.u.sint = 1;
1243 return 1;
1244}
1245
1246/* return a valid test if the current request is the first one on the connection */
1247static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1248{
Willy Tarreau79512b62020-04-29 11:52:13 +02001249 if (!smp->strm)
1250 return 0;
1251
Willy Tarreau79e57332018-10-02 16:01:16 +02001252 smp->data.type = SMP_T_BOOL;
1253 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1254 return 1;
1255}
1256
Christopher Fauleta4063562019-08-02 11:51:37 +02001257/* Fetch the authentication method if there is an Authorization header. It
1258 * relies on get_http_auth()
1259 */
1260static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1261{
1262 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001263 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001264 struct http_txn *txn;
1265
1266 if (!htx)
1267 return 0;
1268
1269 txn = smp->strm->txn;
1270 if (!get_http_auth(smp, htx))
1271 return 0;
1272
1273 switch (txn->auth.method) {
1274 case HTTP_AUTH_BASIC:
1275 smp->data.u.str.area = "Basic";
1276 smp->data.u.str.data = 5;
1277 break;
1278 case HTTP_AUTH_DIGEST:
1279 /* Unexpected because not supported */
1280 smp->data.u.str.area = "Digest";
1281 smp->data.u.str.data = 6;
1282 break;
1283 default:
1284 return 0;
1285 }
1286
1287 smp->data.type = SMP_T_STR;
1288 smp->flags = SMP_F_CONST;
1289 return 1;
1290}
1291
1292/* Fetch the user supplied if there is an Authorization header. It relies on
1293 * get_http_auth()
1294 */
1295static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1296{
1297 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001298 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001299 struct http_txn *txn;
1300
1301 if (!htx)
1302 return 0;
1303
1304 txn = smp->strm->txn;
1305 if (!get_http_auth(smp, htx))
1306 return 0;
1307
1308 smp->data.type = SMP_T_STR;
1309 smp->data.u.str.area = txn->auth.user;
1310 smp->data.u.str.data = strlen(txn->auth.user);
1311 smp->flags = SMP_F_CONST;
1312 return 1;
1313}
1314
1315/* Fetch the password supplied if there is an Authorization header. It relies on
1316 * get_http_auth()
1317 */
1318static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1319{
1320 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001321 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001322 struct http_txn *txn;
1323
1324 if (!htx)
1325 return 0;
1326
1327 txn = smp->strm->txn;
1328 if (!get_http_auth(smp, htx))
1329 return 0;
1330
1331 smp->data.type = SMP_T_STR;
1332 smp->data.u.str.area = txn->auth.pass;
1333 smp->data.u.str.data = strlen(txn->auth.pass);
1334 smp->flags = SMP_F_CONST;
1335 return 1;
1336}
1337
Willy Tarreau79e57332018-10-02 16:01:16 +02001338/* Accepts exactly 1 argument of type userlist */
1339static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1340{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001341 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001342 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001343
Christopher Faulet623af932021-01-29 11:22:15 +01001344 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001345 return 0;
1346
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001347 if (!htx)
1348 return 0;
1349 if (!get_http_auth(smp, htx))
1350 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001351
1352 smp->data.type = SMP_T_BOOL;
1353 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001354 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001355 return 1;
1356}
1357
1358/* Accepts exactly 1 argument of type userlist */
1359static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1360{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001361 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001362 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001363
Christopher Faulet623af932021-01-29 11:22:15 +01001364 if (args->type != ARGT_USR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001365 return 0;
1366
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001367 if (!htx)
1368 return 0;
1369 if (!get_http_auth(smp, htx))
1370 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001371
Willy Tarreau79e57332018-10-02 16:01:16 +02001372 /* if the user does not belong to the userlist or has a wrong password,
1373 * report that it unconditionally does not match. Otherwise we return
1374 * a string containing the username.
1375 */
1376 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1377 smp->strm->txn->auth.pass))
1378 return 0;
1379
1380 /* pat_match_auth() will need the user list */
1381 smp->ctx.a[0] = args->data.usr;
1382
1383 smp->data.type = SMP_T_STR;
1384 smp->flags = SMP_F_CONST;
1385 smp->data.u.str.area = smp->strm->txn->auth.user;
1386 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1387
1388 return 1;
1389}
1390
1391/* Fetch a captured HTTP request header. The index is the position of
1392 * the "capture" option in the configuration file
1393 */
1394static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1395{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001396 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001397 int idx;
1398
Christopher Faulet623af932021-01-29 11:22:15 +01001399 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001400 return 0;
1401
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001402 if (!smp->strm)
1403 return 0;
1404
1405 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001406 idx = args->data.sint;
1407
1408 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1409 return 0;
1410
1411 smp->data.type = SMP_T_STR;
1412 smp->flags |= SMP_F_CONST;
1413 smp->data.u.str.area = smp->strm->req_cap[idx];
1414 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1415
1416 return 1;
1417}
1418
1419/* Fetch a captured HTTP response header. The index is the position of
1420 * the "capture" option in the configuration file
1421 */
1422static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1423{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001424 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001425 int idx;
1426
Christopher Faulet623af932021-01-29 11:22:15 +01001427 if (args->type != ARGT_SINT)
Willy Tarreau79e57332018-10-02 16:01:16 +02001428 return 0;
1429
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001430 if (!smp->strm)
1431 return 0;
1432
1433 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001434 idx = args->data.sint;
1435
1436 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1437 return 0;
1438
1439 smp->data.type = SMP_T_STR;
1440 smp->flags |= SMP_F_CONST;
1441 smp->data.u.str.area = smp->strm->res_cap[idx];
1442 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1443
1444 return 1;
1445}
1446
1447/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1448static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1449{
1450 struct buffer *temp;
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001451 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001452 char *ptr;
1453
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001454 if (!smp->strm)
1455 return 0;
1456
1457 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001458 if (!txn || !txn->uri)
1459 return 0;
1460
1461 ptr = txn->uri;
1462
1463 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1464 ptr++;
1465
1466 temp = get_trash_chunk();
1467 temp->area = txn->uri;
1468 temp->data = ptr - txn->uri;
1469 smp->data.u.str = *temp;
1470 smp->data.type = SMP_T_STR;
1471 smp->flags = SMP_F_CONST;
1472
1473 return 1;
1474
1475}
1476
1477/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1478static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1479{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001480 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001481 struct ist path;
1482 const char *ptr;
1483
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001484 if (!smp->strm)
1485 return 0;
1486
1487 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001488 if (!txn || !txn->uri)
1489 return 0;
1490
1491 ptr = txn->uri;
1492
1493 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1494 ptr++;
1495
1496 if (!*ptr)
1497 return 0;
1498
Christopher Faulet78337bb2018-11-15 14:35:18 +01001499 /* skip the first space and find space after URI */
1500 path = ist2(++ptr, 0);
1501 while (*ptr != ' ' && *ptr != '\0')
1502 ptr++;
1503 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001504
Christopher Faulet78337bb2018-11-15 14:35:18 +01001505 path = http_get_path(path);
Tim Duesterhused526372020-03-05 17:56:33 +01001506 if (!isttest(path))
Willy Tarreau79e57332018-10-02 16:01:16 +02001507 return 0;
1508
1509 smp->data.u.str.area = path.ptr;
1510 smp->data.u.str.data = path.len;
1511 smp->data.type = SMP_T_STR;
1512 smp->flags = SMP_F_CONST;
1513
1514 return 1;
1515}
1516
1517/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1518 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1519 */
1520static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1521{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001522 struct http_txn *txn;
1523
1524 if (!smp->strm)
1525 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001526
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001527 txn = smp->strm->txn;
Christopher Faulet09f88362021-04-01 16:00:29 +02001528 if (!txn || txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001529 return 0;
1530
1531 if (txn->req.flags & HTTP_MSGF_VER_11)
1532 smp->data.u.str.area = "HTTP/1.1";
1533 else
1534 smp->data.u.str.area = "HTTP/1.0";
1535
1536 smp->data.u.str.data = 8;
1537 smp->data.type = SMP_T_STR;
1538 smp->flags = SMP_F_CONST;
1539 return 1;
1540
1541}
1542
1543/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1544 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1545 */
1546static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1547{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001548 struct http_txn *txn;
1549
1550 if (!smp->strm)
1551 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001552
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001553 txn = smp->strm->txn;
Christopher Faulet09f88362021-04-01 16:00:29 +02001554 if (!txn || txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001555 return 0;
1556
1557 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1558 smp->data.u.str.area = "HTTP/1.1";
1559 else
1560 smp->data.u.str.area = "HTTP/1.0";
1561
1562 smp->data.u.str.data = 8;
1563 smp->data.type = SMP_T_STR;
1564 smp->flags = SMP_F_CONST;
1565 return 1;
1566
1567}
1568
1569/* Iterate over all cookies present in a message. The context is stored in
1570 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1571 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1572 * the direction, multiple cookies may be parsed on the same line or not.
Maciej Zdebdea7c202020-11-13 09:38:06 +00001573 * If provided, the searched cookie name is in args, in args->data.str. If
1574 * the input options indicate that no iterating is desired, then only last
1575 * value is fetched if any. If no cookie name is provided, the first cookie
1576 * value found is fetched. The returned sample is of type CSTR. Can be used
1577 * to parse cookies in other files.
Willy Tarreau79e57332018-10-02 16:01:16 +02001578 */
1579static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1580{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001581 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1582 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001583 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001584 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001585 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1586 struct ist hdr;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001587 char *cook = NULL;
1588 size_t cook_l = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001589 int found = 0;
1590
Christopher Faulet623af932021-01-29 11:22:15 +01001591 if (args->type == ARGT_STR) {
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001592 cook = args->data.str.area;
1593 cook_l = args->data.str.data;
1594 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001595
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001596 if (!ctx) {
1597 /* first call */
1598 ctx = &static_http_hdr_ctx;
1599 ctx->blk = NULL;
1600 smp->ctx.a[2] = ctx;
1601 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001602
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001603 if (!htx)
1604 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001605
Christopher Faulet16032ab2020-04-30 11:30:00 +02001606 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001607
Maciej Zdebdea7c202020-11-13 09:38:06 +00001608 /* OK so basically here, either we want only one value or we want to
1609 * iterate over all of them and we fetch the next one. In this last case
1610 * SMP_OPT_ITERATE option is set.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001611 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001612
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001613 if (!(smp->flags & SMP_F_NOT_LAST)) {
1614 /* search for the header from the beginning, we must first initialize
1615 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001616 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001617 smp->ctx.a[0] = NULL;
1618 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001619 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001620
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001621 smp->flags |= SMP_F_VOL_HDR;
1622 while (1) {
1623 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1624 if (!smp->ctx.a[0]) {
1625 if (!http_find_header(htx, hdr, ctx, 0))
1626 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001627
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001628 if (ctx->value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001629 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001630
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001631 smp->ctx.a[0] = ctx->value.ptr;
1632 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001633 }
1634
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001635 smp->data.type = SMP_T_STR;
1636 smp->flags |= SMP_F_CONST;
1637 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001638 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001639 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1640 &smp->data.u.str.area,
1641 &smp->data.u.str.data);
1642 if (smp->ctx.a[0]) {
1643 found = 1;
Maciej Zdebdea7c202020-11-13 09:38:06 +00001644 if (smp->opt & SMP_OPT_ITERATE) {
1645 /* iterate on cookie value */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001646 smp->flags |= SMP_F_NOT_LAST;
1647 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001648 }
Maciej Zdebdea7c202020-11-13 09:38:06 +00001649 if (args->data.str.data == 0) {
1650 /* No cookie name, first occurrence returned */
1651 break;
1652 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001653 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001654 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001655 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001656
Willy Tarreau79e57332018-10-02 16:01:16 +02001657 /* all cookie headers and values were scanned. If we're looking for the
1658 * last occurrence, we may return it now.
1659 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001660 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001661 smp->flags &= ~SMP_F_NOT_LAST;
1662 return found;
1663}
1664
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001665/* Same than smp_fetch_cookie() but only relies on the sample direction to
1666 * choose the right channel. So instead of duplicating the code, we just change
1667 * the keyword and then fallback on smp_fetch_cookie().
1668 */
1669static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1670{
1671 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1672 return smp_fetch_cookie(args, smp, kw, private);
1673}
1674
Willy Tarreau79e57332018-10-02 16:01:16 +02001675/* Iterate over all cookies present in a request to count how many occurrences
1676 * match the name in args and args->data.str.len. If <multi> is non-null, then
1677 * multiple cookies may be parsed on the same line. The returned sample is of
1678 * type UINT. Accepts exactly 1 argument of type string.
1679 */
1680static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1681{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001682 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1683 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001684 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001685 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001686 struct http_hdr_ctx ctx;
1687 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001688 char *val_beg, *val_end;
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001689 char *cook = NULL;
1690 size_t cook_l = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001691 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001692
Christopher Faulet623af932021-01-29 11:22:15 +01001693 if (args->type == ARGT_STR){
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001694 cook = args->data.str.area;
1695 cook_l = args->data.str.data;
1696 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001697
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001698 if (!htx)
1699 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001700
Christopher Faulet16032ab2020-04-30 11:30:00 +02001701 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001702
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001703 val_end = val_beg = NULL;
1704 ctx.blk = NULL;
1705 cnt = 0;
1706 while (1) {
1707 /* Note: val_beg == NULL every time we need to fetch a new header */
1708 if (!val_beg) {
1709 if (!http_find_header(htx, hdr, &ctx, 0))
1710 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001711
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001712 if (ctx.value.len < cook_l + 1)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001713 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001714
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001715 val_beg = ctx.value.ptr;
1716 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001717 }
1718
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001719 smp->data.type = SMP_T_STR;
1720 smp->flags |= SMP_F_CONST;
1721 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
Christopher Faulet97fc8da2020-11-13 13:41:04 +01001722 cook, cook_l,
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001723 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1724 &smp->data.u.str.area,
1725 &smp->data.u.str.data))) {
1726 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001727 }
1728 }
1729
1730 smp->data.type = SMP_T_SINT;
1731 smp->data.u.sint = cnt;
1732 smp->flags |= SMP_F_VOL_HDR;
1733 return 1;
1734}
1735
1736/* Fetch an cookie's integer value. The integer value is returned. It
1737 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1738 */
1739static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1740{
1741 int ret = smp_fetch_cookie(args, smp, kw, private);
1742
1743 if (ret > 0) {
1744 smp->data.type = SMP_T_SINT;
1745 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1746 smp->data.u.str.data);
1747 }
1748
1749 return ret;
1750}
1751
1752/************************************************************************/
1753/* The code below is dedicated to sample fetches */
1754/************************************************************************/
1755
1756/* This scans a URL-encoded query string. It takes an optionally wrapping
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001757 * string whose first contiguous chunk has its beginning in ctx->a[0] and end
Willy Tarreau79e57332018-10-02 16:01:16 +02001758 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1759 * pointers are updated for next iteration before leaving.
1760 */
1761static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1762{
1763 const char *vstart, *vend;
1764 struct buffer *temp;
1765 const char **chunks = (const char **)smp->ctx.a;
1766
1767 if (!http_find_next_url_param(chunks, name, name_len,
1768 &vstart, &vend, delim))
1769 return 0;
1770
1771 /* Create sample. If the value is contiguous, return the pointer as CONST,
1772 * if the value is wrapped, copy-it in a buffer.
1773 */
1774 smp->data.type = SMP_T_STR;
1775 if (chunks[2] &&
1776 vstart >= chunks[0] && vstart <= chunks[1] &&
1777 vend >= chunks[2] && vend <= chunks[3]) {
1778 /* Wrapped case. */
1779 temp = get_trash_chunk();
1780 memcpy(temp->area, vstart, chunks[1] - vstart);
1781 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1782 vend - chunks[2]);
1783 smp->data.u.str.area = temp->area;
1784 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1785 } else {
1786 /* Contiguous case. */
1787 smp->data.u.str.area = (char *)vstart;
1788 smp->data.u.str.data = vend - vstart;
1789 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1790 }
1791
1792 /* Update context, check wrapping. */
1793 chunks[0] = vend;
1794 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1795 chunks[1] = chunks[3];
1796 chunks[2] = NULL;
1797 }
1798
1799 if (chunks[0] < chunks[1])
1800 smp->flags |= SMP_F_NOT_LAST;
1801
1802 return 1;
1803}
1804
1805/* This function iterates over each parameter of the query string. It uses
1806 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1807 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1808 * An optional parameter name is passed in args[0], otherwise any parameter is
1809 * considered. It supports an optional delimiter argument for the beginning of
1810 * the string in args[1], which defaults to "?".
1811 */
1812static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1813{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001814 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001815 char delim = '?';
1816 const char *name;
1817 int name_len;
1818
Christopher Faulet623af932021-01-29 11:22:15 +01001819 if ((args[0].type && args[0].type != ARGT_STR) ||
Willy Tarreau79e57332018-10-02 16:01:16 +02001820 (args[1].type && args[1].type != ARGT_STR))
1821 return 0;
1822
1823 name = "";
1824 name_len = 0;
1825 if (args->type == ARGT_STR) {
1826 name = args->data.str.area;
1827 name_len = args->data.str.data;
1828 }
1829
1830 if (args[1].type)
1831 delim = *args[1].data.str.area;
1832
1833 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001834 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001835 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001836
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001837 if (!htx)
1838 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001839
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001840 sl = http_get_stline(htx);
1841 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1842 if (!smp->ctx.a[0])
1843 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001844
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001845 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001846
1847 /* Assume that the context is filled with NULL pointer
1848 * before the first call.
1849 * smp->ctx.a[2] = NULL;
1850 * smp->ctx.a[3] = NULL;
1851 */
1852 }
1853
1854 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1855}
1856
1857/* This function iterates over each parameter of the body. This requires
1858 * that the body has been waited for using http-buffer-request. It uses
1859 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001860 * contiguous part of the body, and optionally ctx->a[2..3] to reference the
Willy Tarreau79e57332018-10-02 16:01:16 +02001861 * optional second part if the body wraps at the end of the buffer. An optional
1862 * parameter name is passed in args[0], otherwise any parameter is considered.
1863 */
1864static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1865{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001866 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001867 const char *name;
1868 int name_len;
1869
Christopher Faulet623af932021-01-29 11:22:15 +01001870 if (args[0].type && args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +02001871 return 0;
1872
1873 name = "";
1874 name_len = 0;
1875 if (args[0].type == ARGT_STR) {
1876 name = args[0].data.str.area;
1877 name_len = args[0].data.str.data;
1878 }
1879
1880 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulete596d182020-05-05 17:46:34 +02001881 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001882 struct buffer *temp;
1883 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001884
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001885 if (!htx)
1886 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001887
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001888 temp = get_trash_chunk();
1889 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1890 struct htx_blk *blk = htx_get_blk(htx, pos);
1891 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001892
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001893 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001894 break;
1895 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001896 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001897 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001898 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001899 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001900
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001901 smp->ctx.a[0] = temp->area;
1902 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001903
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001904 /* Assume that the context is filled with NULL pointer
1905 * before the first call.
1906 * smp->ctx.a[2] = NULL;
1907 * smp->ctx.a[3] = NULL;
1908 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001909
Willy Tarreau79e57332018-10-02 16:01:16 +02001910 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001911
Willy Tarreau79e57332018-10-02 16:01:16 +02001912 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1913}
1914
1915/* Return the signed integer value for the specified url parameter (see url_param
1916 * above).
1917 */
1918static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1919{
1920 int ret = smp_fetch_url_param(args, smp, kw, private);
1921
1922 if (ret > 0) {
1923 smp->data.type = SMP_T_SINT;
1924 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1925 smp->data.u.str.data);
1926 }
1927
1928 return ret;
1929}
1930
1931/* This produces a 32-bit hash of the concatenation of the first occurrence of
1932 * the Host header followed by the path component if it begins with a slash ('/').
1933 * This means that '*' will not be added, resulting in exactly the first Host
1934 * entry. If no Host header is found, then the path is used. The resulting value
1935 * is hashed using the url hash followed by a full avalanche hash and provides a
1936 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1937 * high-traffic sites without having to store whole paths.
1938 * this differs from the base32 functions in that it includes the url parameters
1939 * as well as the path
1940 */
1941static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1942{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001943 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001944 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001945 struct http_hdr_ctx ctx;
1946 struct htx_sl *sl;
1947 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001948 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001949
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001950 if (!htx)
1951 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001952
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001953 ctx.blk = NULL;
1954 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1955 /* OK we have the header value in ctx.value */
1956 while (ctx.value.len--)
1957 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001958 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001959
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001960 /* now retrieve the path */
1961 sl = http_get_stline(htx);
1962 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001963 if (path.len && *(path.ptr) == '/') {
1964 while (path.len--)
1965 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001966 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001967
Willy Tarreau79e57332018-10-02 16:01:16 +02001968 hash = full_hash(hash);
1969
1970 smp->data.type = SMP_T_SINT;
1971 smp->data.u.sint = hash;
1972 smp->flags = SMP_F_VOL_1ST;
1973 return 1;
1974}
1975
1976/* This concatenates the source address with the 32-bit hash of the Host and
1977 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1978 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1979 * on the source address length. The URL hash is stored before the address so
1980 * that in environments where IPv6 is insignificant, truncating the output to
1981 * 8 bytes would still work.
1982 */
1983static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1984{
1985 struct buffer *temp;
1986 struct connection *cli_conn = objt_conn(smp->sess->origin);
1987
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001988 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001989 return 0;
1990
1991 if (!smp_fetch_url32(args, smp, kw, private))
1992 return 0;
1993
1994 temp = get_trash_chunk();
1995 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1996 temp->data += sizeof(unsigned int);
1997
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001998 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001999 case AF_INET:
2000 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002001 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002002 4);
2003 temp->data += 4;
2004 break;
2005 case AF_INET6:
2006 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02002007 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002008 16);
2009 temp->data += 16;
2010 break;
2011 default:
2012 return 0;
2013 }
2014
2015 smp->data.u.str = *temp;
2016 smp->data.type = SMP_T_BIN;
2017 return 1;
2018}
2019
2020/************************************************************************/
2021/* Other utility functions */
2022/************************************************************************/
2023
2024/* This function is used to validate the arguments passed to any "hdr" fetch
2025 * keyword. These keywords support an optional positive or negative occurrence
2026 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2027 * is assumed that the types are already the correct ones. Returns 0 on error,
2028 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2029 * error message in case of error, that the caller is responsible for freeing.
2030 * The initial location must either be freeable or NULL.
2031 * Note: this function's pointer is checked from Lua.
2032 */
2033int val_hdr(struct arg *arg, char **err_msg)
2034{
2035 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2036 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2037 return 0;
2038 }
2039 return 1;
2040}
2041
2042/************************************************************************/
2043/* All supported sample fetch keywords must be declared here. */
2044/************************************************************************/
2045
2046/* Note: must not be declared <const> as its list will be overwritten */
2047static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2048 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2049 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2050 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Yves Lafonb4d37082021-02-11 11:01:28 +01002051 { "baseq", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002052
2053 /* capture are allocated and are permanent in the stream */
2054 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2055
2056 /* retrieve these captures from the HTTP logs */
2057 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2058 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2059 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2060
2061 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2062 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2063
2064 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2065 * are only here to match the ACL's name, are request-only and are used
2066 * for ACL compatibility only.
2067 */
2068 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002069 { "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 +02002070 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2071 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2072
2073 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2074 * only here to match the ACL's name, are request-only and are used for
2075 * ACL compatibility only.
2076 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002077 { "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 +02002078 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2079 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2080 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2081
Christopher Fauleta4063562019-08-02 11:51:37 +02002082 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2083 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2084 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002085 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2086 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2087 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2088 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2089 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Faulete720c322020-09-02 17:25:18 +02002090 { "pathq", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002091 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2092
2093 /* HTTP protocol on the request path */
2094 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2095 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2096
2097 /* HTTP version on the request path */
2098 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2099 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2100
2101 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2102 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2103 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2104 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2105
2106 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2107 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2108
2109 /* HTTP version on the response path */
2110 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2111 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2112
Christopher Faulete596d182020-05-05 17:46:34 +02002113 { "res.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2114 { "res.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2115 { "res.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2116
2117 { "res.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2118 { "res.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2119
Willy Tarreau79e57332018-10-02 16:01:16 +02002120 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2121 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2122 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2123 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2124
2125 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2126 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2127 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2128 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2129 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2130 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2131 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2132
2133 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2134 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2135 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2136 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2137
2138 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2139 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2140 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2141 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2142 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2143 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2144 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2145
2146 /* scook is valid only on the response and is used for ACL compatibility */
2147 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2148 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2149 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2150 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2151
2152 /* shdr is valid only on the response and is used for ACL compatibility */
2153 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2154 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2155 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2156 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2157
2158 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2159 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2160 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2161 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2162 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2163 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2164 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2165 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2166 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2167 { "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 +02002168
Willy Tarreau79e57332018-10-02 16:01:16 +02002169 { /* END */ },
2170}};
2171
Willy Tarreau0108d902018-11-25 19:14:37 +01002172INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002173
2174/*
2175 * Local variables:
2176 * c-indent-level: 8
2177 * c-basic-offset: 8
2178 * End:
2179 */