blob: 460bfbd349404563045efbce99d1f44366f332d5 [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{
71 free(static_raw_htx_buf);
72 static_raw_htx_buf = NULL;
73}
74
75REGISTER_PER_THREAD_ALLOC(alloc_raw_htx_chunk_per_thread);
76REGISTER_PER_THREAD_FREE(free_raw_htx_chunk_per_thread);
77
Willy Tarreau79e57332018-10-02 16:01:16 +020078/*
79 * Returns the data from Authorization header. Function may be called more
80 * than once so data is stored in txn->auth_data. When no header is found
81 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
82 * searching again for something we are unable to find anyway. However, if
83 * the result if valid, the cache is not reused because we would risk to
84 * have the credentials overwritten by another stream in parallel.
Willy Tarreaueae83722020-04-29 11:52:51 +020085 * The caller is responsible for passing a sample with a valid stream/txn,
86 * and a valid htx.
Willy Tarreau79e57332018-10-02 16:01:16 +020087 */
88
Christopher Fauletcd761952019-07-15 13:58:29 +020089static int get_http_auth(struct sample *smp, struct htx *htx)
Willy Tarreau79e57332018-10-02 16:01:16 +020090{
Christopher Faulet311c7ea2018-10-24 21:41:55 +020091 struct stream *s = smp->strm;
Willy Tarreau79e57332018-10-02 16:01:16 +020092 struct http_txn *txn = s->txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020093 struct http_hdr_ctx ctx = { .blk = NULL };
94 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +020095 struct buffer auth_method;
Christopher Faulet6d1dd462019-07-15 14:36:03 +020096 char *p;
Willy Tarreau79e57332018-10-02 16:01:16 +020097 int len;
98
99#ifdef DEBUG_AUTH
100 printf("Auth for stream %p: %d\n", s, txn->auth.method);
101#endif
Willy Tarreau79e57332018-10-02 16:01:16 +0200102 if (txn->auth.method == HTTP_AUTH_WRONG)
103 return 0;
104
105 txn->auth.method = HTTP_AUTH_WRONG;
106
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200107 if (txn->flags & TX_USE_PX_CONN)
108 hdr = ist("Proxy-Authorization");
109 else
110 hdr = ist("Authorization");
Willy Tarreau79e57332018-10-02 16:01:16 +0200111
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200112 ctx.blk = NULL;
113 if (!http_find_header(htx, hdr, &ctx, 0))
114 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200115
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200116 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
117 len = p - ctx.value.ptr;
118 if (!p || len <= 0)
119 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200120
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200121 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
122 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200123
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200124 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200125
126 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
127 struct buffer *http_auth = get_trash_chunk();
128
129 len = base64dec(txn->auth.method_data.area,
130 txn->auth.method_data.data,
131 http_auth->area, global.tune.bufsize - 1);
132
133 if (len < 0)
134 return 0;
135
136
137 http_auth->area[len] = '\0';
138
139 p = strchr(http_auth->area, ':');
140
141 if (!p)
142 return 0;
143
144 txn->auth.user = http_auth->area;
145 *p = '\0';
146 txn->auth.pass = p+1;
147
148 txn->auth.method = HTTP_AUTH_BASIC;
149 return 1;
150 }
151
152 return 0;
153}
154
155/* This function ensures that the prerequisites for an L7 fetch are ready,
156 * which means that a request or response is ready. If some data is missing,
157 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200158 * to extract data from L7. If <vol> is non-null during a prefetch, another
159 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200160 *
161 * The function returns :
162 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
163 * decide whether or not an HTTP message is present ;
164 * NULL if the requested data cannot be fetched or if it is certain that
Willy Tarreaueae83722020-04-29 11:52:51 +0200165 * we'll never have any HTTP message there; this includes null strm or chn.
Willy Tarreaua6d98792020-08-12 14:04:52 +0200166 * NULL if the sample's direction does not match the channel's (i.e. the
167 * function was asked to work on the wrong channel)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200168 * The HTX message if ready
169 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200170struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, struct check *check, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200171{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200172 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200173 struct http_txn *txn = NULL;
174 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200175 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100176 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200177
Willy Tarreaua6d98792020-08-12 14:04:52 +0200178 if (chn &&
179 (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ && (chn->flags & CF_ISRESP)) ||
180 ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES && !(chn->flags & CF_ISRESP))))
181 return 0;
182
Christopher Fauletef453ed2018-10-24 21:39:27 +0200183 /* Note: it is possible that <s> is NULL when called before stream
184 * initialization (eg: tcp-request connection), so this function is the
185 * one responsible for guarding against this case for all HTTP users.
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200186 *
187 * In the health check context, the stream and the channel must be NULL
188 * and <check> must be set. In this case, only the input buffer,
189 * corresponding to the response, is considered. It is the caller
190 * responsibility to provide <check>.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200191 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200192 BUG_ON(check && (s || chn));
193 if (!s || !chn) {
194 if (check) {
195 htx = htxbuf(&check->bi);
196
197 /* Analyse not yet started */
198 if (htx_is_empty(htx) || htx->first == -1)
199 return NULL;
200
201 sl = http_get_stline(htx);
202 if (vol && !sl) {
203 /* The start-line was already forwarded, it is too late to fetch anything */
204 return NULL;
205 }
206 goto end;
207 }
208
Christopher Fauletef453ed2018-10-24 21:39:27 +0200209 return NULL;
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200210 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200211
212 if (!s->txn) {
213 if (unlikely(!http_alloc_txn(s)))
214 return NULL; /* not enough memory */
215 http_init_txn(s);
216 txn = s->txn;
217 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200218 txn = s->txn;
219 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
220 smp->data.type = SMP_T_BOOL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200221
Christopher Fauleteca88542019-04-03 10:12:42 +0200222 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200223 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200224
Christopher Faulet89dc4992019-04-17 12:02:59 +0200225 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
226 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200227
Christopher Faulet89dc4992019-04-17 12:02:59 +0200228 if (msg->msg_state < HTTP_MSG_BODY) {
229 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200230 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200231 /* Parsing is done by the mux, just wait */
232 smp->flags |= SMP_F_MAY_CHANGE;
233 return NULL;
234 }
235 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200236 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200237 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200238 /* The start-line was already forwarded, it is too late to fetch anything */
239 return NULL;
240 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200241 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200242 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200243 struct buffer *buf;
244 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200245 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200246 union h1_sl h1sl;
247 unsigned int flags = HTX_FL_NONE;
248 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200249
Christopher Faulet89dc4992019-04-17 12:02:59 +0200250 /* no HTTP fetch on the response in TCP mode */
251 if (chn->flags & CF_ISRESP)
252 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200253
Christopher Faulet89dc4992019-04-17 12:02:59 +0200254 /* Now we are working on the request only */
255 buf = &chn->buf;
256 if (b_head(buf) + b_data(buf) > b_wrap(buf))
257 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200258
Christopher Faulet89dc4992019-04-17 12:02:59 +0200259 h1m_init_req(&h1m);
260 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
261 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
262 if (ret <= 0) {
263 /* Invalid or too big*/
264 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200265 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100266
Christopher Faulet89dc4992019-04-17 12:02:59 +0200267 /* wait for a full request */
268 smp->flags |= SMP_F_MAY_CHANGE;
269 return NULL;
270 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100271
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500272 /* OK we just got a valid HTTP message. We have to convert it
Christopher Faulet89dc4992019-04-17 12:02:59 +0200273 * into an HTX message.
274 */
275 if (unlikely(h1sl.rq.v.len == 0)) {
276 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
277 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200278 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200279 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200280 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200281
282 /* Set HTX start-line flags */
283 if (h1m.flags & H1_MF_VER_11)
284 flags |= HTX_SL_F_VER_11;
285 if (h1m.flags & H1_MF_XFER_ENC)
286 flags |= HTX_SL_F_XFER_ENC;
287 flags |= HTX_SL_F_XFER_LEN;
288 if (h1m.flags & H1_MF_CHNK)
289 flags |= HTX_SL_F_CHNK;
290 else if (h1m.flags & H1_MF_CLEN)
291 flags |= HTX_SL_F_CLEN;
292
Richard Russo458eafb2019-07-31 11:45:56 -0700293 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200294 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
295 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200296 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200297 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200298 }
299
300 /* OK we just got a valid HTTP message. If not already done by
301 * HTTP analyzers, we have some minor preparation to perform so
302 * that further checks can rely on HTTP tests.
303 */
304 if (sl && msg->msg_state < HTTP_MSG_BODY) {
305 if (!(chn->flags & CF_ISRESP)) {
306 txn->meth = sl->info.req.meth;
307 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
308 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200309 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200310 else
311 txn->status = sl->info.res.status;
312 if (sl->flags & HTX_SL_F_VER_11)
313 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200314 }
315
316 /* everything's OK */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200317 end:
Christopher Fauletef453ed2018-10-24 21:39:27 +0200318 smp->data.u.sint = 1;
319 return htx;
320}
321
Willy Tarreau79e57332018-10-02 16:01:16 +0200322/* This function fetches the method of current HTTP request and stores
323 * it in the global pattern struct as a chunk. There are two possibilities :
324 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
325 * in <len> and <ptr> is NULL ;
326 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
327 * <len> to its length.
328 * This is intended to be used with pat_match_meth() only.
329 */
330static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
331{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200332 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200333 struct http_txn *txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200334 int meth;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200335
Willy Tarreaua6d98792020-08-12 14:04:52 +0200336 txn = smp->strm->txn;
337 if (!txn)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200338 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200339
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200340 meth = txn->meth;
341 smp->data.type = SMP_T_METH;
342 smp->data.u.meth.meth = meth;
343 if (meth == HTTP_METH_OTHER) {
Willy Tarreaua6d98792020-08-12 14:04:52 +0200344 struct htx *htx;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200345 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200346
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200347 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
348 /* ensure the indexes are not affected */
349 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200350 }
Willy Tarreaua6d98792020-08-12 14:04:52 +0200351
352 htx = smp_prefetch_htx(smp, chn, NULL, 0);
353 if (!htx)
354 return 0;
355
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200356 sl = http_get_stline(htx);
357 smp->flags |= SMP_F_CONST;
358 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
359 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200360 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200361 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200362 return 1;
363}
364
365static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
366{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200367 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200368 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200369 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200370 char *ptr;
371 int len;
372
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200373 if (!htx)
374 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200375
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200376 sl = http_get_stline(htx);
377 len = HTX_SL_REQ_VLEN(sl);
378 ptr = HTX_SL_REQ_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200379
380 while ((len-- > 0) && (*ptr++ != '/'));
381 if (len <= 0)
382 return 0;
383
384 smp->data.type = SMP_T_STR;
385 smp->data.u.str.area = ptr;
386 smp->data.u.str.data = len;
387
388 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
389 return 1;
390}
391
392static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
393{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200394 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200395 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200396 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200397 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200398 char *ptr;
399 int len;
400
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200401 if (!htx)
402 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200403
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200404 sl = http_get_stline(htx);
405 len = HTX_SL_RES_VLEN(sl);
406 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200407
408 while ((len-- > 0) && (*ptr++ != '/'));
409 if (len <= 0)
410 return 0;
411
412 smp->data.type = SMP_T_STR;
413 smp->data.u.str.area = ptr;
414 smp->data.u.str.data = len;
415
416 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
417 return 1;
418}
419
420/* 3. Check on Status Code. We manipulate integers here. */
421static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
422{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200423 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200424 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200425 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200426 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200427 char *ptr;
428 int len;
429
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200430 if (!htx)
431 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200432
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200433 sl = http_get_stline(htx);
434 len = HTX_SL_RES_CLEN(sl);
435 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200436
437 smp->data.type = SMP_T_SINT;
438 smp->data.u.sint = __strl2ui(ptr, len);
439 smp->flags = SMP_F_VOL_1ST;
440 return 1;
441}
442
443static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
444{
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100445 struct ist unique_id;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100446
Willy Tarreau79e57332018-10-02 16:01:16 +0200447 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
448 return 0;
449
Willy Tarreaua1062a42020-04-29 11:50:38 +0200450 if (!smp->strm)
451 return 0;
452
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100453 unique_id = stream_generate_unique_id(smp->strm, &smp->sess->fe->format_unique_id);
454 if (!isttest(unique_id))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100455 return 0;
456
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100457 smp->data.u.str.area = smp->strm->unique_id.ptr;
458 smp->data.u.str.data = smp->strm->unique_id.len;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100459 smp->data.type = SMP_T_STR;
Willy Tarreau79e57332018-10-02 16:01:16 +0200460 smp->flags = SMP_F_CONST;
461 return 1;
462}
463
464/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800465 * empty line which separes headers from the body. This is useful
466 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200467 */
468static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
469{
Christopher Faulete596d182020-05-05 17:46:34 +0200470 /* possible keywords: req.hdrs, res.hdrs */
471 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200472 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200473 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200474 struct buffer *temp;
475 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200476
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200477 if (!htx)
478 return 0;
479 temp = get_trash_chunk();
480 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
481 struct htx_blk *blk = htx_get_blk(htx, pos);
482 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200483
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200484 if (type == HTX_BLK_HDR) {
485 struct ist n = htx_get_blk_name(htx, blk);
486 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200487
Christopher Faulet53a899b2019-10-08 16:38:42 +0200488 if (!h1_format_htx_hdr(n, v, temp))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200489 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200490 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200491 else if (type == HTX_BLK_EOH) {
492 if (!chunk_memcat(temp, "\r\n", 2))
493 return 0;
494 break;
495 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200496 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200497 smp->data.type = SMP_T_STR;
498 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200499 return 1;
500}
501
502/* Returns the header request in a length/value encoded format.
503 * This is useful for exchanges with the SPOE.
504 *
505 * A "length value" is a multibyte code encoding numbers. It uses the
506 * SPOE format. The encoding is the following:
507 *
508 * Each couple "header name" / "header value" is composed
509 * like this:
510 * "length value" "header name bytes"
511 * "length value" "header value bytes"
512 * When the last header is reached, the header name and the header
513 * value are empty. Their length are 0
514 */
515static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
516{
Christopher Faulete596d182020-05-05 17:46:34 +0200517 /* possible keywords: req.hdrs_bin, res.hdrs_bin */
518 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200519 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200520 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200521 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200522 char *p, *end;
523 int32_t pos;
524 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200525
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200526 if (!htx)
527 return 0;
528 temp = get_trash_chunk();
529 p = temp->area;
530 end = temp->area + temp->size;
531 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
532 struct htx_blk *blk = htx_get_blk(htx, pos);
533 enum htx_blk_type type = htx_get_blk_type(blk);
534 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200535
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200536 if (type == HTX_BLK_HDR) {
537 n = htx_get_blk_name(htx,blk);
538 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200539
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200540 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200541 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200542 if (ret == -1)
543 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200544 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200545 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200546 memcpy(p, n.ptr, n.len);
547 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200548
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200549 /* encode the header value. */
550 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200551 if (ret == -1)
552 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200553 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200554 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200555 memcpy(p, v.ptr, v.len);
556 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200557
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200558 }
559 else if (type == HTX_BLK_EOH) {
560 /* encode the end of the header list with empty
561 * header name and header value.
562 */
563 ret = encode_varint(0, &p, end);
564 if (ret == -1)
565 return 0;
566 ret = encode_varint(0, &p, end);
567 if (ret == -1)
568 return 0;
569 break;
570 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200571 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200572
573 /* Initialise sample data which will be filled. */
574 smp->data.type = SMP_T_BIN;
575 smp->data.u.str.area = temp->area;
576 smp->data.u.str.data = p - temp->area;
577 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200578 return 1;
579}
580
581/* returns the longest available part of the body. This requires that the body
582 * has been waited for using http-buffer-request.
583 */
584static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
585{
Christopher Faulete596d182020-05-05 17:46:34 +0200586 /* possible keywords: req.body, res.body */
587 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200588 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200589 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200590 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200591 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200592
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200593 if (!htx)
594 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200595
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200596 temp = get_trash_chunk();
597 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
598 struct htx_blk *blk = htx_get_blk(htx, pos);
599 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200600
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200601 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
602 break;
603 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +0200604 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200605 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200606 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200607 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200608
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200609 smp->data.type = SMP_T_BIN;
610 smp->data.u.str = *temp;
611 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200612
613 if (!channel_full(chn, global.tune.maxrewrite) && !(chn->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)))
614 smp->flags |= SMP_F_MAY_CHANGE;
615
Willy Tarreau79e57332018-10-02 16:01:16 +0200616 return 1;
617}
618
619
620/* returns the available length of the body. This requires that the body
621 * has been waited for using http-buffer-request.
622 */
623static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
624{
Christopher Faulete596d182020-05-05 17:46:34 +0200625 /* possible keywords: req.body_len, res.body_len */
626 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200627 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200628 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200629 int32_t pos;
630 unsigned long long len = 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100631
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200632 if (!htx)
633 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100634
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200635 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
636 struct htx_blk *blk = htx_get_blk(htx, pos);
637 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100638
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200639 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
640 break;
641 if (type == HTX_BLK_DATA)
642 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200643 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200644
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200645 smp->data.type = SMP_T_SINT;
646 smp->data.u.sint = len;
647 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200648 return 1;
649}
650
651
652/* returns the advertised length of the body, or the advertised size of the
653 * chunks available in the buffer. This requires that the body has been waited
654 * for using http-buffer-request.
655 */
656static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
657{
Christopher Faulete596d182020-05-05 17:46:34 +0200658 /* possible keywords: req.body_size, res.body_size */
659 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200660 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200661 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200662 int32_t pos;
663 unsigned long long len = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200664
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200665 if (!htx)
666 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100667
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200668 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
669 struct htx_blk *blk = htx_get_blk(htx, pos);
670 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100671
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200672 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
673 break;
674 if (type == HTX_BLK_DATA)
675 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200676 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200677 if (htx->extra != ULLONG_MAX)
678 len += htx->extra;
Willy Tarreau79e57332018-10-02 16:01:16 +0200679
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200680 smp->data.type = SMP_T_SINT;
681 smp->data.u.sint = len;
682 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200683 return 1;
684}
685
686
687/* 4. Check on URL/URI. A pointer to the URI is stored. */
688static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
689{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200690 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200691 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200692 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200693
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200694 if (!htx)
695 return 0;
696 sl = http_get_stline(htx);
697 smp->data.type = SMP_T_STR;
698 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
699 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
700 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200701 return 1;
702}
703
704static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
705{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200706 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200707 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200708 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200709 struct sockaddr_storage addr;
710
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200711 if (!htx)
712 return 0;
713 sl = http_get_stline(htx);
714 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +0200715
Willy Tarreau79e57332018-10-02 16:01:16 +0200716 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
717 return 0;
718
719 smp->data.type = SMP_T_IPV4;
720 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
721 smp->flags = 0;
722 return 1;
723}
724
725static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
726{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200727 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200728 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200729 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200730 struct sockaddr_storage addr;
731
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200732 if (!htx)
733 return 0;
734 sl = http_get_stline(htx);
735 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200736
Willy Tarreau79e57332018-10-02 16:01:16 +0200737 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
738 return 0;
739
740 smp->data.type = SMP_T_SINT;
741 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
742 smp->flags = 0;
743 return 1;
744}
745
746/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
747 * Accepts an optional argument of type string containing the header field name,
748 * and an optional argument of type signed or unsigned integer to request an
749 * explicit occurrence of the header. Note that in the event of a missing name,
750 * headers are considered from the first one. It does not stop on commas and
751 * returns full lines instead (useful for User-Agent or Date for example).
752 */
753static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
754{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200755 /* possible keywords: req.fhdr, res.fhdr */
756 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200757 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200758 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200759 struct http_hdr_ctx *ctx = smp->ctx.a[0];
760 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200761 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200762
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200763 if (!ctx) {
764 /* first call */
765 ctx = &static_http_hdr_ctx;
766 ctx->blk = NULL;
767 smp->ctx.a[0] = ctx;
768 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200769
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200770 if (args) {
771 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200772 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200773 name.ptr = args[0].data.str.area;
774 name.len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +0200775
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200776 if (args[1].type == ARGT_SINT)
777 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200778 }
779
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200780 if (!htx)
781 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200782
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200783 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
784 /* search for header from the beginning */
785 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200786
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200787 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
788 /* no explicit occurrence and single fetch => last header by default */
789 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200790
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200791 if (!occ)
792 /* prepare to report multiple occurrences for ACL fetches */
793 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200794
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200795 smp->data.type = SMP_T_STR;
796 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
797 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
798 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200799 smp->flags &= ~SMP_F_NOT_LAST;
800 return 0;
801}
802
803/* 6. Check on HTTP header count. The number of occurrences is returned.
804 * Accepts exactly 1 argument of type string. It does not stop on commas and
805 * returns full lines instead (useful for User-Agent or Date for example).
806 */
807static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
808{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200809 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
810 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200811 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200812 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200813 struct http_hdr_ctx ctx;
814 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200815 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200816
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200817 if (!htx)
818 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200819
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200820 if (args && args->type == ARGT_STR) {
821 name.ptr = args->data.str.area;
822 name.len = args->data.str.data;
823 } else {
824 name.ptr = NULL;
825 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200826 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200827
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200828 ctx.blk = NULL;
829 cnt = 0;
830 while (http_find_header(htx, name, &ctx, 1))
831 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200832 smp->data.type = SMP_T_SINT;
833 smp->data.u.sint = cnt;
834 smp->flags = SMP_F_VOL_HDR;
835 return 1;
836}
837
838static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
839{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200840 /* possible keywords: req.hdr_names, res.hdr_names */
841 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200842 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200843 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200844 struct buffer *temp;
845 char del = ',';
846
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200847 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200848
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200849 if (!htx)
850 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200851
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200852 if (args && args->type == ARGT_STR)
853 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200854
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200855 temp = get_trash_chunk();
856 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
857 struct htx_blk *blk = htx_get_blk(htx, pos);
858 enum htx_blk_type type = htx_get_blk_type(blk);
859 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200860
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200861 if (type == HTX_BLK_EOH)
862 break;
863 if (type != HTX_BLK_HDR)
864 continue;
865 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200866
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200867 if (temp->data)
868 temp->area[temp->data++] = del;
869 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200870 }
871
872 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200873 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200874 smp->flags = SMP_F_VOL_HDR;
875 return 1;
876}
877
878/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
879 * Accepts an optional argument of type string containing the header field name,
880 * and an optional argument of type signed or unsigned integer to request an
881 * explicit occurrence of the header. Note that in the event of a missing name,
882 * headers are considered from the first one.
883 */
884static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
885{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200886 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
887 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200888 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200889 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200890 struct http_hdr_ctx *ctx = smp->ctx.a[0];
891 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200892 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200893
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200894 if (!ctx) {
895 /* first call */
896 ctx = &static_http_hdr_ctx;
897 ctx->blk = NULL;
898 smp->ctx.a[0] = ctx;
899 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200900
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200901 if (args) {
902 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200903 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200904 name.ptr = args[0].data.str.area;
905 name.len = args[0].data.str.data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200906
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200907 if (args[1].type == ARGT_SINT)
908 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200909 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200910
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200911 if (!htx)
912 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200913
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200914 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
915 /* search for header from the beginning */
916 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200917
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200918 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
919 /* no explicit occurrence and single fetch => last header by default */
920 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200921
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200922 if (!occ)
923 /* prepare to report multiple occurrences for ACL fetches */
924 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200925
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200926 smp->data.type = SMP_T_STR;
927 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
928 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
929 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200930
931 smp->flags &= ~SMP_F_NOT_LAST;
932 return 0;
933}
934
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200935/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
936 * the right channel. So instead of duplicating the code, we just change the
937 * keyword and then fallback on smp_fetch_hdr().
938 */
939static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
940{
941 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
942 return smp_fetch_hdr(args, smp, kw, private);
943}
944
Willy Tarreau79e57332018-10-02 16:01:16 +0200945/* 6. Check on HTTP header count. The number of occurrences is returned.
946 * Accepts exactly 1 argument of type string.
947 */
948static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
949{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200950 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
951 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200952 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200953 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200954 struct http_hdr_ctx ctx;
955 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200956 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200957
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200958 if (!htx)
959 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200960
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200961 if (args && args->type == ARGT_STR) {
962 name.ptr = args->data.str.area;
963 name.len = args->data.str.data;
964 } else {
965 name.ptr = NULL;
966 name.len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200967 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200968
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200969 ctx.blk = NULL;
970 cnt = 0;
971 while (http_find_header(htx, name, &ctx, 0))
972 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200973
974 smp->data.type = SMP_T_SINT;
975 smp->data.u.sint = cnt;
976 smp->flags = SMP_F_VOL_HDR;
977 return 1;
978}
979
980/* Fetch an HTTP header's integer value. The integer value is returned. It
981 * takes a mandatory argument of type string and an optional one of type int
982 * to designate a specific occurrence. It returns an unsigned integer, which
983 * may or may not be appropriate for everything.
984 */
985static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
986{
987 int ret = smp_fetch_hdr(args, smp, kw, private);
988
989 if (ret > 0) {
990 smp->data.type = SMP_T_SINT;
991 smp->data.u.sint = strl2ic(smp->data.u.str.area,
992 smp->data.u.str.data);
993 }
994
995 return ret;
996}
997
998/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
999 * and an optional one of type int to designate a specific occurrence.
1000 * It returns an IPv4 or IPv6 address.
1001 */
1002static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1003{
1004 int ret;
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001005 struct buffer *temp = get_trash_chunk();
Willy Tarreau79e57332018-10-02 16:01:16 +02001006
1007 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001008 if (smp->data.u.str.data < temp->size - 1) {
1009 memcpy(temp->area, smp->data.u.str.area,
1010 smp->data.u.str.data);
1011 temp->area[smp->data.u.str.data] = '\0';
1012 if (url2ipv4((char *) temp->area, &smp->data.u.ipv4)) {
1013 smp->data.type = SMP_T_IPV4;
1014 break;
1015 } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1016 smp->data.type = SMP_T_IPV6;
1017 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001018 }
1019 }
1020
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001021 /* if the header doesn't match an IP address, fetch next one */
1022 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001023 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001024 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001025 return ret;
1026}
Willy Tarreau79e57332018-10-02 16:01:16 +02001027
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001028/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1029 * the first '/' after the possible hostname, and ends before the possible '?'.
1030 */
1031static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1032{
1033 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001034 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001035 struct htx_sl *sl;
1036 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001037
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001038 if (!htx)
1039 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001040
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001041 sl = http_get_stline(htx);
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001042 path = iststop(http_get_path(htx_sl_req_uri(sl)), '?');
Tim Duesterhused526372020-03-05 17:56:33 +01001043 if (!isttest(path))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001044 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001045
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001046 /* OK, we got the '/' ! */
1047 smp->data.type = SMP_T_STR;
1048 smp->data.u.str.area = path.ptr;
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001049 smp->data.u.str.data = path.len;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001050 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001051 return 1;
1052}
1053
1054/* This produces a concatenation of the first occurrence of the Host header
1055 * followed by the path component if it begins with a slash ('/'). This means
1056 * that '*' will not be added, resulting in exactly the first Host entry.
1057 * If no Host header is found, then the path is returned as-is. The returned
1058 * value is stored in the trash so it does not need to be marked constant.
1059 * The returned sample is of type string.
1060 */
1061static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1062{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001063 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001064 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001065 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001066 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001067 struct http_hdr_ctx ctx;
1068 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001069
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001070 if (!htx)
1071 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001072
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001073 ctx.blk = NULL;
1074 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1075 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001076
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001077 /* OK we have the header value in ctx.value */
1078 temp = get_trash_chunk();
1079 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001080
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001081 /* now retrieve the path */
1082 sl = http_get_stline(htx);
1083 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001084 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001085 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001086
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001087 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1088 ;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001089
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001090 if (len && *(path.ptr) == '/')
1091 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001092 }
1093
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001094 smp->data.type = SMP_T_STR;
1095 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001096 smp->flags = SMP_F_VOL_1ST;
1097 return 1;
1098}
1099
1100/* This produces a 32-bit hash of the concatenation of the first occurrence of
1101 * the Host header followed by the path component if it begins with a slash ('/').
1102 * This means that '*' will not be added, resulting in exactly the first Host
1103 * entry. If no Host header is found, then the path is used. The resulting value
1104 * is hashed using the path hash followed by a full avalanche hash and provides a
1105 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1106 * high-traffic sites without having to store whole paths.
1107 */
1108static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1109{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001110 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001111 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001112 struct htx_sl *sl;
1113 struct http_hdr_ctx ctx;
1114 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001115 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001116
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001117 if (!htx)
1118 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001119
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001120 ctx.blk = NULL;
1121 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1122 /* OK we have the header value in ctx.value */
1123 while (ctx.value.len--)
1124 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001125 }
1126
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001127 /* now retrieve the path */
1128 sl = http_get_stline(htx);
1129 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001130 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001131 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001132
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001133 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1134 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001135
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001136 if (len && *(path.ptr) == '/') {
1137 while (len--)
1138 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001139 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001140 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001141
Willy Tarreau79e57332018-10-02 16:01:16 +02001142 hash = full_hash(hash);
1143
1144 smp->data.type = SMP_T_SINT;
1145 smp->data.u.sint = hash;
1146 smp->flags = SMP_F_VOL_1ST;
1147 return 1;
1148}
1149
1150/* This concatenates the source address with the 32-bit hash of the Host and
1151 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1152 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1153 * on the source address length. The path hash is stored before the address so
1154 * that in environments where IPv6 is insignificant, truncating the output to
1155 * 8 bytes would still work.
1156 */
1157static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1158{
1159 struct buffer *temp;
1160 struct connection *cli_conn = objt_conn(smp->sess->origin);
1161
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001162 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001163 return 0;
1164
1165 if (!smp_fetch_base32(args, smp, kw, private))
1166 return 0;
1167
1168 temp = get_trash_chunk();
1169 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1170 temp->data += sizeof(unsigned int);
1171
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001172 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001173 case AF_INET:
1174 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001175 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001176 4);
1177 temp->data += 4;
1178 break;
1179 case AF_INET6:
1180 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001181 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001182 16);
1183 temp->data += 16;
1184 break;
1185 default:
1186 return 0;
1187 }
1188
1189 smp->data.u.str = *temp;
1190 smp->data.type = SMP_T_BIN;
1191 return 1;
1192}
1193
1194/* Extracts the query string, which comes after the question mark '?'. If no
1195 * question mark is found, nothing is returned. Otherwise it returns a sample
1196 * of type string carrying the whole query string.
1197 */
1198static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1199{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001200 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001201 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001202 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001203 char *ptr, *end;
1204
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001205 if (!htx)
1206 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001207
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001208 sl = http_get_stline(htx);
1209 ptr = HTX_SL_REQ_UPTR(sl);
1210 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001211
1212 /* look up the '?' */
1213 do {
1214 if (ptr == end)
1215 return 0;
1216 } while (*ptr++ != '?');
1217
1218 smp->data.type = SMP_T_STR;
1219 smp->data.u.str.area = ptr;
1220 smp->data.u.str.data = end - ptr;
1221 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1222 return 1;
1223}
1224
1225static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1226{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001227 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001228 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001229
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001230 if (!htx)
1231 return 0;
1232 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001233 smp->data.u.sint = 1;
1234 return 1;
1235}
1236
1237/* return a valid test if the current request is the first one on the connection */
1238static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1239{
Willy Tarreau79512b62020-04-29 11:52:13 +02001240 if (!smp->strm)
1241 return 0;
1242
Willy Tarreau79e57332018-10-02 16:01:16 +02001243 smp->data.type = SMP_T_BOOL;
1244 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1245 return 1;
1246}
1247
Christopher Fauleta4063562019-08-02 11:51:37 +02001248/* Fetch the authentication method if there is an Authorization header. It
1249 * relies on get_http_auth()
1250 */
1251static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1252{
1253 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001254 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001255 struct http_txn *txn;
1256
1257 if (!htx)
1258 return 0;
1259
1260 txn = smp->strm->txn;
1261 if (!get_http_auth(smp, htx))
1262 return 0;
1263
1264 switch (txn->auth.method) {
1265 case HTTP_AUTH_BASIC:
1266 smp->data.u.str.area = "Basic";
1267 smp->data.u.str.data = 5;
1268 break;
1269 case HTTP_AUTH_DIGEST:
1270 /* Unexpected because not supported */
1271 smp->data.u.str.area = "Digest";
1272 smp->data.u.str.data = 6;
1273 break;
1274 default:
1275 return 0;
1276 }
1277
1278 smp->data.type = SMP_T_STR;
1279 smp->flags = SMP_F_CONST;
1280 return 1;
1281}
1282
1283/* Fetch the user supplied if there is an Authorization header. It relies on
1284 * get_http_auth()
1285 */
1286static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1287{
1288 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001289 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001290 struct http_txn *txn;
1291
1292 if (!htx)
1293 return 0;
1294
1295 txn = smp->strm->txn;
1296 if (!get_http_auth(smp, htx))
1297 return 0;
1298
1299 smp->data.type = SMP_T_STR;
1300 smp->data.u.str.area = txn->auth.user;
1301 smp->data.u.str.data = strlen(txn->auth.user);
1302 smp->flags = SMP_F_CONST;
1303 return 1;
1304}
1305
1306/* Fetch the password supplied if there is an Authorization header. It relies on
1307 * get_http_auth()
1308 */
1309static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1310{
1311 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001312 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001313 struct http_txn *txn;
1314
1315 if (!htx)
1316 return 0;
1317
1318 txn = smp->strm->txn;
1319 if (!get_http_auth(smp, htx))
1320 return 0;
1321
1322 smp->data.type = SMP_T_STR;
1323 smp->data.u.str.area = txn->auth.pass;
1324 smp->data.u.str.data = strlen(txn->auth.pass);
1325 smp->flags = SMP_F_CONST;
1326 return 1;
1327}
1328
Willy Tarreau79e57332018-10-02 16:01:16 +02001329/* Accepts exactly 1 argument of type userlist */
1330static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1331{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001332 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001333 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001334
1335 if (!args || args->type != ARGT_USR)
1336 return 0;
1337
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001338 if (!htx)
1339 return 0;
1340 if (!get_http_auth(smp, htx))
1341 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001342
1343 smp->data.type = SMP_T_BOOL;
1344 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001345 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001346 return 1;
1347}
1348
1349/* Accepts exactly 1 argument of type userlist */
1350static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1351{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001352 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001353 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001354
Willy Tarreau79e57332018-10-02 16:01:16 +02001355 if (!args || args->type != ARGT_USR)
1356 return 0;
1357
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001358 if (!htx)
1359 return 0;
1360 if (!get_http_auth(smp, htx))
1361 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001362
Willy Tarreau79e57332018-10-02 16:01:16 +02001363 /* if the user does not belong to the userlist or has a wrong password,
1364 * report that it unconditionally does not match. Otherwise we return
1365 * a string containing the username.
1366 */
1367 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1368 smp->strm->txn->auth.pass))
1369 return 0;
1370
1371 /* pat_match_auth() will need the user list */
1372 smp->ctx.a[0] = args->data.usr;
1373
1374 smp->data.type = SMP_T_STR;
1375 smp->flags = SMP_F_CONST;
1376 smp->data.u.str.area = smp->strm->txn->auth.user;
1377 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1378
1379 return 1;
1380}
1381
1382/* Fetch a captured HTTP request header. The index is the position of
1383 * the "capture" option in the configuration file
1384 */
1385static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1386{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001387 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001388 int idx;
1389
1390 if (!args || args->type != ARGT_SINT)
1391 return 0;
1392
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001393 if (!smp->strm)
1394 return 0;
1395
1396 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001397 idx = args->data.sint;
1398
1399 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1400 return 0;
1401
1402 smp->data.type = SMP_T_STR;
1403 smp->flags |= SMP_F_CONST;
1404 smp->data.u.str.area = smp->strm->req_cap[idx];
1405 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1406
1407 return 1;
1408}
1409
1410/* Fetch a captured HTTP response header. The index is the position of
1411 * the "capture" option in the configuration file
1412 */
1413static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1414{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001415 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001416 int idx;
1417
1418 if (!args || args->type != ARGT_SINT)
1419 return 0;
1420
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001421 if (!smp->strm)
1422 return 0;
1423
1424 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001425 idx = args->data.sint;
1426
1427 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1428 return 0;
1429
1430 smp->data.type = SMP_T_STR;
1431 smp->flags |= SMP_F_CONST;
1432 smp->data.u.str.area = smp->strm->res_cap[idx];
1433 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1434
1435 return 1;
1436}
1437
1438/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1439static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1440{
1441 struct buffer *temp;
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001442 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001443 char *ptr;
1444
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001445 if (!smp->strm)
1446 return 0;
1447
1448 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001449 if (!txn || !txn->uri)
1450 return 0;
1451
1452 ptr = txn->uri;
1453
1454 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1455 ptr++;
1456
1457 temp = get_trash_chunk();
1458 temp->area = txn->uri;
1459 temp->data = ptr - txn->uri;
1460 smp->data.u.str = *temp;
1461 smp->data.type = SMP_T_STR;
1462 smp->flags = SMP_F_CONST;
1463
1464 return 1;
1465
1466}
1467
1468/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1469static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1470{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001471 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001472 struct ist path;
1473 const char *ptr;
1474
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001475 if (!smp->strm)
1476 return 0;
1477
1478 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001479 if (!txn || !txn->uri)
1480 return 0;
1481
1482 ptr = txn->uri;
1483
1484 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1485 ptr++;
1486
1487 if (!*ptr)
1488 return 0;
1489
Christopher Faulet78337bb2018-11-15 14:35:18 +01001490 /* skip the first space and find space after URI */
1491 path = ist2(++ptr, 0);
1492 while (*ptr != ' ' && *ptr != '\0')
1493 ptr++;
1494 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001495
Christopher Faulet78337bb2018-11-15 14:35:18 +01001496 path = http_get_path(path);
Tim Duesterhused526372020-03-05 17:56:33 +01001497 if (!isttest(path))
Willy Tarreau79e57332018-10-02 16:01:16 +02001498 return 0;
1499
1500 smp->data.u.str.area = path.ptr;
1501 smp->data.u.str.data = path.len;
1502 smp->data.type = SMP_T_STR;
1503 smp->flags = SMP_F_CONST;
1504
1505 return 1;
1506}
1507
1508/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1509 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1510 */
1511static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1512{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001513 struct http_txn *txn;
1514
1515 if (!smp->strm)
1516 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001517
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001518 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001519 if (!txn || txn->req.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001520 return 0;
1521
1522 if (txn->req.flags & HTTP_MSGF_VER_11)
1523 smp->data.u.str.area = "HTTP/1.1";
1524 else
1525 smp->data.u.str.area = "HTTP/1.0";
1526
1527 smp->data.u.str.data = 8;
1528 smp->data.type = SMP_T_STR;
1529 smp->flags = SMP_F_CONST;
1530 return 1;
1531
1532}
1533
1534/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1535 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1536 */
1537static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1538{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001539 struct http_txn *txn;
1540
1541 if (!smp->strm)
1542 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001543
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001544 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001545 if (!txn || txn->rsp.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001546 return 0;
1547
1548 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1549 smp->data.u.str.area = "HTTP/1.1";
1550 else
1551 smp->data.u.str.area = "HTTP/1.0";
1552
1553 smp->data.u.str.data = 8;
1554 smp->data.type = SMP_T_STR;
1555 smp->flags = SMP_F_CONST;
1556 return 1;
1557
1558}
1559
1560/* Iterate over all cookies present in a message. The context is stored in
1561 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1562 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1563 * the direction, multiple cookies may be parsed on the same line or not.
1564 * The cookie name is in args and the name length in args->data.str.len.
1565 * Accepts exactly 1 argument of type string. If the input options indicate
1566 * that no iterating is desired, then only last value is fetched if any.
1567 * The returned sample is of type CSTR. Can be used to parse cookies in other
1568 * files.
1569 */
1570static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1571{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001572 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1573 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001574 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001575 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001576 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1577 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001578 int occ = 0;
1579 int found = 0;
1580
1581 if (!args || args->type != ARGT_STR)
1582 return 0;
1583
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001584 if (!ctx) {
1585 /* first call */
1586 ctx = &static_http_hdr_ctx;
1587 ctx->blk = NULL;
1588 smp->ctx.a[2] = ctx;
1589 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001590
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001591 if (!htx)
1592 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001593
Christopher Faulet16032ab2020-04-30 11:30:00 +02001594 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001595
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001596 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1597 /* no explicit occurrence and single fetch => last cookie by default */
1598 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001599
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001600 /* OK so basically here, either we want only one value and it's the
1601 * last one, or we want to iterate over all of them and we fetch the
1602 * next one.
1603 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001604
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001605 if (!(smp->flags & SMP_F_NOT_LAST)) {
1606 /* search for the header from the beginning, we must first initialize
1607 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001608 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001609 smp->ctx.a[0] = NULL;
1610 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001611 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001612
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001613 smp->flags |= SMP_F_VOL_HDR;
1614 while (1) {
1615 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1616 if (!smp->ctx.a[0]) {
1617 if (!http_find_header(htx, hdr, ctx, 0))
1618 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001619
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001620 if (ctx->value.len < args->data.str.data + 1)
1621 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001622
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001623 smp->ctx.a[0] = ctx->value.ptr;
1624 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001625 }
1626
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001627 smp->data.type = SMP_T_STR;
1628 smp->flags |= SMP_F_CONST;
1629 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
1630 args->data.str.area, args->data.str.data,
1631 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1632 &smp->data.u.str.area,
1633 &smp->data.u.str.data);
1634 if (smp->ctx.a[0]) {
1635 found = 1;
1636 if (occ >= 0) {
1637 /* one value was returned into smp->data.u.str.{str,len} */
1638 smp->flags |= SMP_F_NOT_LAST;
1639 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001640 }
1641 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001642 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001643 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001644
Willy Tarreau79e57332018-10-02 16:01:16 +02001645 /* all cookie headers and values were scanned. If we're looking for the
1646 * last occurrence, we may return it now.
1647 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001648 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001649 smp->flags &= ~SMP_F_NOT_LAST;
1650 return found;
1651}
1652
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001653/* Same than smp_fetch_cookie() but only relies on the sample direction to
1654 * choose the right channel. So instead of duplicating the code, we just change
1655 * the keyword and then fallback on smp_fetch_cookie().
1656 */
1657static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1658{
1659 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1660 return smp_fetch_cookie(args, smp, kw, private);
1661}
1662
Willy Tarreau79e57332018-10-02 16:01:16 +02001663/* Iterate over all cookies present in a request to count how many occurrences
1664 * match the name in args and args->data.str.len. If <multi> is non-null, then
1665 * multiple cookies may be parsed on the same line. The returned sample is of
1666 * type UINT. Accepts exactly 1 argument of type string.
1667 */
1668static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1669{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001670 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1671 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001672 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001673 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001674 struct http_hdr_ctx ctx;
1675 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001676 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001677 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001678
1679 if (!args || args->type != ARGT_STR)
1680 return 0;
1681
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001682 if (!htx)
1683 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001684
Christopher Faulet16032ab2020-04-30 11:30:00 +02001685 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001686
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001687 val_end = val_beg = NULL;
1688 ctx.blk = NULL;
1689 cnt = 0;
1690 while (1) {
1691 /* Note: val_beg == NULL every time we need to fetch a new header */
1692 if (!val_beg) {
1693 if (!http_find_header(htx, hdr, &ctx, 0))
1694 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001695
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001696 if (ctx.value.len < args->data.str.data + 1)
1697 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001698
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001699 val_beg = ctx.value.ptr;
1700 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001701 }
1702
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001703 smp->data.type = SMP_T_STR;
1704 smp->flags |= SMP_F_CONST;
1705 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
1706 args->data.str.area, args->data.str.data,
1707 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1708 &smp->data.u.str.area,
1709 &smp->data.u.str.data))) {
1710 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001711 }
1712 }
1713
1714 smp->data.type = SMP_T_SINT;
1715 smp->data.u.sint = cnt;
1716 smp->flags |= SMP_F_VOL_HDR;
1717 return 1;
1718}
1719
1720/* Fetch an cookie's integer value. The integer value is returned. It
1721 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1722 */
1723static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1724{
1725 int ret = smp_fetch_cookie(args, smp, kw, private);
1726
1727 if (ret > 0) {
1728 smp->data.type = SMP_T_SINT;
1729 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1730 smp->data.u.str.data);
1731 }
1732
1733 return ret;
1734}
1735
1736/************************************************************************/
1737/* The code below is dedicated to sample fetches */
1738/************************************************************************/
1739
1740/* This scans a URL-encoded query string. It takes an optionally wrapping
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001741 * string whose first contiguous chunk has its beginning in ctx->a[0] and end
Willy Tarreau79e57332018-10-02 16:01:16 +02001742 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1743 * pointers are updated for next iteration before leaving.
1744 */
1745static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1746{
1747 const char *vstart, *vend;
1748 struct buffer *temp;
1749 const char **chunks = (const char **)smp->ctx.a;
1750
1751 if (!http_find_next_url_param(chunks, name, name_len,
1752 &vstart, &vend, delim))
1753 return 0;
1754
1755 /* Create sample. If the value is contiguous, return the pointer as CONST,
1756 * if the value is wrapped, copy-it in a buffer.
1757 */
1758 smp->data.type = SMP_T_STR;
1759 if (chunks[2] &&
1760 vstart >= chunks[0] && vstart <= chunks[1] &&
1761 vend >= chunks[2] && vend <= chunks[3]) {
1762 /* Wrapped case. */
1763 temp = get_trash_chunk();
1764 memcpy(temp->area, vstart, chunks[1] - vstart);
1765 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1766 vend - chunks[2]);
1767 smp->data.u.str.area = temp->area;
1768 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1769 } else {
1770 /* Contiguous case. */
1771 smp->data.u.str.area = (char *)vstart;
1772 smp->data.u.str.data = vend - vstart;
1773 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1774 }
1775
1776 /* Update context, check wrapping. */
1777 chunks[0] = vend;
1778 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1779 chunks[1] = chunks[3];
1780 chunks[2] = NULL;
1781 }
1782
1783 if (chunks[0] < chunks[1])
1784 smp->flags |= SMP_F_NOT_LAST;
1785
1786 return 1;
1787}
1788
1789/* This function iterates over each parameter of the query string. It uses
1790 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1791 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1792 * An optional parameter name is passed in args[0], otherwise any parameter is
1793 * considered. It supports an optional delimiter argument for the beginning of
1794 * the string in args[1], which defaults to "?".
1795 */
1796static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1797{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001798 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001799 char delim = '?';
1800 const char *name;
1801 int name_len;
1802
1803 if (!args ||
1804 (args[0].type && args[0].type != ARGT_STR) ||
1805 (args[1].type && args[1].type != ARGT_STR))
1806 return 0;
1807
1808 name = "";
1809 name_len = 0;
1810 if (args->type == ARGT_STR) {
1811 name = args->data.str.area;
1812 name_len = args->data.str.data;
1813 }
1814
1815 if (args[1].type)
1816 delim = *args[1].data.str.area;
1817
1818 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001819 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001820 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001821
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001822 if (!htx)
1823 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001824
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001825 sl = http_get_stline(htx);
1826 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1827 if (!smp->ctx.a[0])
1828 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001829
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001830 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001831
1832 /* Assume that the context is filled with NULL pointer
1833 * before the first call.
1834 * smp->ctx.a[2] = NULL;
1835 * smp->ctx.a[3] = NULL;
1836 */
1837 }
1838
1839 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1840}
1841
1842/* This function iterates over each parameter of the body. This requires
1843 * that the body has been waited for using http-buffer-request. It uses
1844 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001845 * contiguous part of the body, and optionally ctx->a[2..3] to reference the
Willy Tarreau79e57332018-10-02 16:01:16 +02001846 * optional second part if the body wraps at the end of the buffer. An optional
1847 * parameter name is passed in args[0], otherwise any parameter is considered.
1848 */
1849static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1850{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001851 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001852 const char *name;
1853 int name_len;
1854
1855 if (!args || (args[0].type && args[0].type != ARGT_STR))
1856 return 0;
1857
1858 name = "";
1859 name_len = 0;
1860 if (args[0].type == ARGT_STR) {
1861 name = args[0].data.str.area;
1862 name_len = args[0].data.str.data;
1863 }
1864
1865 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulete596d182020-05-05 17:46:34 +02001866 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001867 struct buffer *temp;
1868 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001869
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001870 if (!htx)
1871 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001872
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001873 temp = get_trash_chunk();
1874 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1875 struct htx_blk *blk = htx_get_blk(htx, pos);
1876 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001877
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001878 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1879 break;
1880 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001881 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001882 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001883 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001884 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001885
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001886 smp->ctx.a[0] = temp->area;
1887 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001888
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001889 /* Assume that the context is filled with NULL pointer
1890 * before the first call.
1891 * smp->ctx.a[2] = NULL;
1892 * smp->ctx.a[3] = NULL;
1893 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001894
Willy Tarreau79e57332018-10-02 16:01:16 +02001895 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001896
Willy Tarreau79e57332018-10-02 16:01:16 +02001897 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1898}
1899
1900/* Return the signed integer value for the specified url parameter (see url_param
1901 * above).
1902 */
1903static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1904{
1905 int ret = smp_fetch_url_param(args, smp, kw, private);
1906
1907 if (ret > 0) {
1908 smp->data.type = SMP_T_SINT;
1909 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1910 smp->data.u.str.data);
1911 }
1912
1913 return ret;
1914}
1915
1916/* This produces a 32-bit hash of the concatenation of the first occurrence of
1917 * the Host header followed by the path component if it begins with a slash ('/').
1918 * This means that '*' will not be added, resulting in exactly the first Host
1919 * entry. If no Host header is found, then the path is used. The resulting value
1920 * is hashed using the url hash followed by a full avalanche hash and provides a
1921 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1922 * high-traffic sites without having to store whole paths.
1923 * this differs from the base32 functions in that it includes the url parameters
1924 * as well as the path
1925 */
1926static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1927{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001928 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001929 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001930 struct http_hdr_ctx ctx;
1931 struct htx_sl *sl;
1932 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001933 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001934
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001935 if (!htx)
1936 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001937
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001938 ctx.blk = NULL;
1939 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1940 /* OK we have the header value in ctx.value */
1941 while (ctx.value.len--)
1942 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001943 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001944
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001945 /* now retrieve the path */
1946 sl = http_get_stline(htx);
1947 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001948 if (path.len && *(path.ptr) == '/') {
1949 while (path.len--)
1950 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001951 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001952
Willy Tarreau79e57332018-10-02 16:01:16 +02001953 hash = full_hash(hash);
1954
1955 smp->data.type = SMP_T_SINT;
1956 smp->data.u.sint = hash;
1957 smp->flags = SMP_F_VOL_1ST;
1958 return 1;
1959}
1960
1961/* This concatenates the source address with the 32-bit hash of the Host and
1962 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1963 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1964 * on the source address length. The URL hash is stored before the address so
1965 * that in environments where IPv6 is insignificant, truncating the output to
1966 * 8 bytes would still work.
1967 */
1968static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1969{
1970 struct buffer *temp;
1971 struct connection *cli_conn = objt_conn(smp->sess->origin);
1972
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001973 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001974 return 0;
1975
1976 if (!smp_fetch_url32(args, smp, kw, private))
1977 return 0;
1978
1979 temp = get_trash_chunk();
1980 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1981 temp->data += sizeof(unsigned int);
1982
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001983 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001984 case AF_INET:
1985 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001986 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001987 4);
1988 temp->data += 4;
1989 break;
1990 case AF_INET6:
1991 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001992 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001993 16);
1994 temp->data += 16;
1995 break;
1996 default:
1997 return 0;
1998 }
1999
2000 smp->data.u.str = *temp;
2001 smp->data.type = SMP_T_BIN;
2002 return 1;
2003}
2004
2005/************************************************************************/
2006/* Other utility functions */
2007/************************************************************************/
2008
2009/* This function is used to validate the arguments passed to any "hdr" fetch
2010 * keyword. These keywords support an optional positive or negative occurrence
2011 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2012 * is assumed that the types are already the correct ones. Returns 0 on error,
2013 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2014 * error message in case of error, that the caller is responsible for freeing.
2015 * The initial location must either be freeable or NULL.
2016 * Note: this function's pointer is checked from Lua.
2017 */
2018int val_hdr(struct arg *arg, char **err_msg)
2019{
2020 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2021 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2022 return 0;
2023 }
2024 return 1;
2025}
2026
2027/************************************************************************/
2028/* All supported sample fetch keywords must be declared here. */
2029/************************************************************************/
2030
2031/* Note: must not be declared <const> as its list will be overwritten */
2032static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2033 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2034 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2035 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2036
2037 /* capture are allocated and are permanent in the stream */
2038 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2039
2040 /* retrieve these captures from the HTTP logs */
2041 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2042 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2043 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2044
2045 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2046 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2047
2048 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2049 * are only here to match the ACL's name, are request-only and are used
2050 * for ACL compatibility only.
2051 */
2052 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002053 { "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 +02002054 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2055 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2056
2057 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2058 * only here to match the ACL's name, are request-only and are used for
2059 * ACL compatibility only.
2060 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002061 { "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 +02002062 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2063 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2064 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2065
Christopher Fauleta4063562019-08-02 11:51:37 +02002066 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2067 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2068 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002069 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2070 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2071 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2072 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2073 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2074 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2075
2076 /* HTTP protocol on the request path */
2077 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2078 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2079
2080 /* HTTP version on the request path */
2081 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2082 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2083
2084 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2085 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2086 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2087 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2088
2089 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2090 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2091
2092 /* HTTP version on the response path */
2093 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2094 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2095
Christopher Faulete596d182020-05-05 17:46:34 +02002096 { "res.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2097 { "res.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2098 { "res.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2099
2100 { "res.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2101 { "res.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2102
Willy Tarreau79e57332018-10-02 16:01:16 +02002103 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2104 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2105 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2106 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2107
2108 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2109 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2110 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2111 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2112 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2113 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2114 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2115
2116 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2117 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2118 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2119 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2120
2121 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2122 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2123 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2124 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2125 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2126 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2127 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2128
2129 /* scook is valid only on the response and is used for ACL compatibility */
2130 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2131 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2132 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2133 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2134
2135 /* shdr is valid only on the response and is used for ACL compatibility */
2136 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2137 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2138 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2139 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2140
2141 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2142 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2143 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2144 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2145 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2146 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2147 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2148 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2149 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2150 { "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 +02002151
Willy Tarreau79e57332018-10-02 16:01:16 +02002152 { /* END */ },
2153}};
2154
Willy Tarreau0108d902018-11-25 19:14:37 +01002155INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002156
2157/*
2158 * Local variables:
2159 * c-indent-level: 8
2160 * c-basic-offset: 8
2161 * End:
2162 */