blob: aece904360da84ad576afd1cb9a900c88ff15123 [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.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200166 * The HTX message if ready
167 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200168struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, struct check *check, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200169{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200170 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200171 struct http_txn *txn = NULL;
172 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200173 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100174 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200175
176 /* Note: it is possible that <s> is NULL when called before stream
177 * initialization (eg: tcp-request connection), so this function is the
178 * one responsible for guarding against this case for all HTTP users.
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200179 *
180 * In the health check context, the stream and the channel must be NULL
181 * and <check> must be set. In this case, only the input buffer,
182 * corresponding to the response, is considered. It is the caller
183 * responsibility to provide <check>.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200184 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200185 BUG_ON(check && (s || chn));
186 if (!s || !chn) {
187 if (check) {
188 htx = htxbuf(&check->bi);
189
190 /* Analyse not yet started */
191 if (htx_is_empty(htx) || htx->first == -1)
192 return NULL;
193
194 sl = http_get_stline(htx);
195 if (vol && !sl) {
196 /* The start-line was already forwarded, it is too late to fetch anything */
197 return NULL;
198 }
199 goto end;
200 }
201
Christopher Fauletef453ed2018-10-24 21:39:27 +0200202 return NULL;
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200203 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200204
205 if (!s->txn) {
206 if (unlikely(!http_alloc_txn(s)))
207 return NULL; /* not enough memory */
208 http_init_txn(s);
209 txn = s->txn;
210 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200211 txn = s->txn;
212 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
213 smp->data.type = SMP_T_BOOL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200214
Christopher Fauleteca88542019-04-03 10:12:42 +0200215 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200216 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200217
Christopher Faulet89dc4992019-04-17 12:02:59 +0200218 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
219 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200220
Christopher Faulet89dc4992019-04-17 12:02:59 +0200221 if (msg->msg_state < HTTP_MSG_BODY) {
222 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200223 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200224 /* Parsing is done by the mux, just wait */
225 smp->flags |= SMP_F_MAY_CHANGE;
226 return NULL;
227 }
228 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200229 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200230 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200231 /* The start-line was already forwarded, it is too late to fetch anything */
232 return NULL;
233 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200234 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200235 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200236 struct buffer *buf;
237 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200238 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200239 union h1_sl h1sl;
240 unsigned int flags = HTX_FL_NONE;
241 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200242
Christopher Faulet89dc4992019-04-17 12:02:59 +0200243 /* no HTTP fetch on the response in TCP mode */
244 if (chn->flags & CF_ISRESP)
245 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200246
Christopher Faulet89dc4992019-04-17 12:02:59 +0200247 /* Now we are working on the request only */
248 buf = &chn->buf;
249 if (b_head(buf) + b_data(buf) > b_wrap(buf))
250 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200251
Christopher Faulet89dc4992019-04-17 12:02:59 +0200252 h1m_init_req(&h1m);
253 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
254 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
255 if (ret <= 0) {
256 /* Invalid or too big*/
257 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200258 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100259
Christopher Faulet89dc4992019-04-17 12:02:59 +0200260 /* wait for a full request */
261 smp->flags |= SMP_F_MAY_CHANGE;
262 return NULL;
263 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100264
Christopher Faulet89dc4992019-04-17 12:02:59 +0200265 /* OK we just got a valid HTTP mesage. We have to convert it
266 * into an HTX message.
267 */
268 if (unlikely(h1sl.rq.v.len == 0)) {
269 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
270 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200271 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200272 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200273 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200274
275 /* Set HTX start-line flags */
276 if (h1m.flags & H1_MF_VER_11)
277 flags |= HTX_SL_F_VER_11;
278 if (h1m.flags & H1_MF_XFER_ENC)
279 flags |= HTX_SL_F_XFER_ENC;
280 flags |= HTX_SL_F_XFER_LEN;
281 if (h1m.flags & H1_MF_CHNK)
282 flags |= HTX_SL_F_CHNK;
283 else if (h1m.flags & H1_MF_CLEN)
284 flags |= HTX_SL_F_CLEN;
285
Richard Russo458eafb2019-07-31 11:45:56 -0700286 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200287 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
288 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200289 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200290 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200291 }
292
293 /* OK we just got a valid HTTP message. If not already done by
294 * HTTP analyzers, we have some minor preparation to perform so
295 * that further checks can rely on HTTP tests.
296 */
297 if (sl && msg->msg_state < HTTP_MSG_BODY) {
298 if (!(chn->flags & CF_ISRESP)) {
299 txn->meth = sl->info.req.meth;
300 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
301 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200302 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200303 else
304 txn->status = sl->info.res.status;
305 if (sl->flags & HTX_SL_F_VER_11)
306 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200307 }
308
309 /* everything's OK */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200310 end:
Christopher Fauletef453ed2018-10-24 21:39:27 +0200311 smp->data.u.sint = 1;
312 return htx;
313}
314
Willy Tarreau79e57332018-10-02 16:01:16 +0200315/* This function fetches the method of current HTTP request and stores
316 * it in the global pattern struct as a chunk. There are two possibilities :
317 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
318 * in <len> and <ptr> is NULL ;
319 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
320 * <len> to its length.
321 * This is intended to be used with pat_match_meth() only.
322 */
323static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
324{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200325 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200326 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +0200327 struct http_txn *txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200328 int meth;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200329
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200330 if (!htx)
331 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200332
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200333 txn = smp->strm->txn;
334 meth = txn->meth;
335 smp->data.type = SMP_T_METH;
336 smp->data.u.meth.meth = meth;
337 if (meth == HTTP_METH_OTHER) {
338 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200339
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200340 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
341 /* ensure the indexes are not affected */
342 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200343 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200344 sl = http_get_stline(htx);
345 smp->flags |= SMP_F_CONST;
346 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
347 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200348 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200349 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200350 return 1;
351}
352
353static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
354{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200355 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200356 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200357 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200358 char *ptr;
359 int len;
360
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200361 if (!htx)
362 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200363
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200364 sl = http_get_stline(htx);
365 len = HTX_SL_REQ_VLEN(sl);
366 ptr = HTX_SL_REQ_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200367
368 while ((len-- > 0) && (*ptr++ != '/'));
369 if (len <= 0)
370 return 0;
371
372 smp->data.type = SMP_T_STR;
373 smp->data.u.str.area = ptr;
374 smp->data.u.str.data = len;
375
376 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
377 return 1;
378}
379
380static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
381{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200382 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200383 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200384 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200385 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200386 char *ptr;
387 int len;
388
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200389 if (!htx)
390 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200391
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200392 sl = http_get_stline(htx);
393 len = HTX_SL_RES_VLEN(sl);
394 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200395
396 while ((len-- > 0) && (*ptr++ != '/'));
397 if (len <= 0)
398 return 0;
399
400 smp->data.type = SMP_T_STR;
401 smp->data.u.str.area = ptr;
402 smp->data.u.str.data = len;
403
404 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
405 return 1;
406}
407
408/* 3. Check on Status Code. We manipulate integers here. */
409static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
410{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200411 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200412 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200413 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200414 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200415 char *ptr;
416 int len;
417
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200418 if (!htx)
419 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200420
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200421 sl = http_get_stline(htx);
422 len = HTX_SL_RES_CLEN(sl);
423 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200424
425 smp->data.type = SMP_T_SINT;
426 smp->data.u.sint = __strl2ui(ptr, len);
427 smp->flags = SMP_F_VOL_1ST;
428 return 1;
429}
430
431static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
432{
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100433 struct ist unique_id;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100434
Willy Tarreau79e57332018-10-02 16:01:16 +0200435 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
436 return 0;
437
Willy Tarreaua1062a42020-04-29 11:50:38 +0200438 if (!smp->strm)
439 return 0;
440
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100441 unique_id = stream_generate_unique_id(smp->strm, &smp->sess->fe->format_unique_id);
442 if (!isttest(unique_id))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100443 return 0;
444
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100445 smp->data.u.str.area = smp->strm->unique_id.ptr;
446 smp->data.u.str.data = smp->strm->unique_id.len;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100447 smp->data.type = SMP_T_STR;
Willy Tarreau79e57332018-10-02 16:01:16 +0200448 smp->flags = SMP_F_CONST;
449 return 1;
450}
451
452/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800453 * empty line which separes headers from the body. This is useful
454 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200455 */
456static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
457{
Christopher Faulete596d182020-05-05 17:46:34 +0200458 /* possible keywords: req.hdrs, res.hdrs */
459 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200460 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200461 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200462 struct buffer *temp;
463 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200464
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200465 if (!htx)
466 return 0;
467 temp = get_trash_chunk();
468 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
469 struct htx_blk *blk = htx_get_blk(htx, pos);
470 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200471
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200472 if (type == HTX_BLK_HDR) {
473 struct ist n = htx_get_blk_name(htx, blk);
474 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200475
Christopher Faulet53a899b2019-10-08 16:38:42 +0200476 if (!h1_format_htx_hdr(n, v, temp))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200477 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200478 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200479 else if (type == HTX_BLK_EOH) {
480 if (!chunk_memcat(temp, "\r\n", 2))
481 return 0;
482 break;
483 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200484 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200485 smp->data.type = SMP_T_STR;
486 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200487 return 1;
488}
489
490/* Returns the header request in a length/value encoded format.
491 * This is useful for exchanges with the SPOE.
492 *
493 * A "length value" is a multibyte code encoding numbers. It uses the
494 * SPOE format. The encoding is the following:
495 *
496 * Each couple "header name" / "header value" is composed
497 * like this:
498 * "length value" "header name bytes"
499 * "length value" "header value bytes"
500 * When the last header is reached, the header name and the header
501 * value are empty. Their length are 0
502 */
503static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
504{
Christopher Faulete596d182020-05-05 17:46:34 +0200505 /* possible keywords: req.hdrs_bin, res.hdrs_bin */
506 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200507 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200508 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200509 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200510 char *p, *end;
511 int32_t pos;
512 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200513
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200514 if (!htx)
515 return 0;
516 temp = get_trash_chunk();
517 p = temp->area;
518 end = temp->area + temp->size;
519 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
520 struct htx_blk *blk = htx_get_blk(htx, pos);
521 enum htx_blk_type type = htx_get_blk_type(blk);
522 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200523
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200524 if (type == HTX_BLK_HDR) {
525 n = htx_get_blk_name(htx,blk);
526 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200527
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200528 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200529 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200530 if (ret == -1)
531 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200532 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200533 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200534 memcpy(p, n.ptr, n.len);
535 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200536
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200537 /* encode the header value. */
538 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200539 if (ret == -1)
540 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200541 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200542 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200543 memcpy(p, v.ptr, v.len);
544 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200545
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200546 }
547 else if (type == HTX_BLK_EOH) {
548 /* encode the end of the header list with empty
549 * header name and header value.
550 */
551 ret = encode_varint(0, &p, end);
552 if (ret == -1)
553 return 0;
554 ret = encode_varint(0, &p, end);
555 if (ret == -1)
556 return 0;
557 break;
558 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200559 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200560
561 /* Initialise sample data which will be filled. */
562 smp->data.type = SMP_T_BIN;
563 smp->data.u.str.area = temp->area;
564 smp->data.u.str.data = p - temp->area;
565 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200566 return 1;
567}
568
569/* returns the longest available part of the body. This requires that the body
570 * has been waited for using http-buffer-request.
571 */
572static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
573{
Christopher Faulete596d182020-05-05 17:46:34 +0200574 /* possible keywords: req.body, res.body */
575 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200576 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200577 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200578 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200579 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200580
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200581 if (!htx)
582 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200583
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200584 temp = get_trash_chunk();
585 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
586 struct htx_blk *blk = htx_get_blk(htx, pos);
587 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200588
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200589 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
590 break;
591 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +0200592 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200593 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200594 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200595 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200596
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200597 smp->data.type = SMP_T_BIN;
598 smp->data.u.str = *temp;
599 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200600
601 if (!channel_full(chn, global.tune.maxrewrite) && !(chn->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)))
602 smp->flags |= SMP_F_MAY_CHANGE;
603
Willy Tarreau79e57332018-10-02 16:01:16 +0200604 return 1;
605}
606
607
608/* returns the available length of the body. This requires that the body
609 * has been waited for using http-buffer-request.
610 */
611static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
612{
Christopher Faulete596d182020-05-05 17:46:34 +0200613 /* possible keywords: req.body_len, res.body_len */
614 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200615 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200616 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200617 int32_t pos;
618 unsigned long long len = 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100619
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200620 if (!htx)
621 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100622
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200623 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
624 struct htx_blk *blk = htx_get_blk(htx, pos);
625 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100626
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200627 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
628 break;
629 if (type == HTX_BLK_DATA)
630 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200631 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200632
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200633 smp->data.type = SMP_T_SINT;
634 smp->data.u.sint = len;
635 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200636 return 1;
637}
638
639
640/* returns the advertised length of the body, or the advertised size of the
641 * chunks available in the buffer. This requires that the body has been waited
642 * for using http-buffer-request.
643 */
644static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
645{
Christopher Faulete596d182020-05-05 17:46:34 +0200646 /* possible keywords: req.body_size, res.body_size */
647 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200648 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200649 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200650 int32_t pos;
651 unsigned long long len = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200652
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200653 if (!htx)
654 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100655
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200656 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
657 struct htx_blk *blk = htx_get_blk(htx, pos);
658 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100659
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200660 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
661 break;
662 if (type == HTX_BLK_DATA)
663 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200664 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200665 if (htx->extra != ULLONG_MAX)
666 len += htx->extra;
Willy Tarreau79e57332018-10-02 16:01:16 +0200667
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200668 smp->data.type = SMP_T_SINT;
669 smp->data.u.sint = len;
670 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200671 return 1;
672}
673
674
675/* 4. Check on URL/URI. A pointer to the URI is stored. */
676static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
677{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200678 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200679 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200680 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200681
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200682 if (!htx)
683 return 0;
684 sl = http_get_stline(htx);
685 smp->data.type = SMP_T_STR;
686 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
687 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
688 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200689 return 1;
690}
691
692static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
693{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200694 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200695 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200696 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200697 struct sockaddr_storage addr;
698
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200699 if (!htx)
700 return 0;
701 sl = http_get_stline(htx);
702 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +0200703
Willy Tarreau79e57332018-10-02 16:01:16 +0200704 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
705 return 0;
706
707 smp->data.type = SMP_T_IPV4;
708 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
709 smp->flags = 0;
710 return 1;
711}
712
713static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
714{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200715 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200716 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200717 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200718 struct sockaddr_storage addr;
719
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200720 if (!htx)
721 return 0;
722 sl = http_get_stline(htx);
723 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200724
Willy Tarreau79e57332018-10-02 16:01:16 +0200725 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
726 return 0;
727
728 smp->data.type = SMP_T_SINT;
729 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
730 smp->flags = 0;
731 return 1;
732}
733
734/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
735 * Accepts an optional argument of type string containing the header field name,
736 * and an optional argument of type signed or unsigned integer to request an
737 * explicit occurrence of the header. Note that in the event of a missing name,
738 * headers are considered from the first one. It does not stop on commas and
739 * returns full lines instead (useful for User-Agent or Date for example).
740 */
741static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
742{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200743 /* possible keywords: req.fhdr, res.fhdr */
744 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200745 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200746 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200747 struct http_hdr_ctx *ctx = smp->ctx.a[0];
748 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200749 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200750
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200751 if (!ctx) {
752 /* first call */
753 ctx = &static_http_hdr_ctx;
754 ctx->blk = NULL;
755 smp->ctx.a[0] = ctx;
756 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200757
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200758 if (args) {
759 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200760 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200761 name.ptr = args[0].data.str.area;
762 name.len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +0200763
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200764 if (args[1].type == ARGT_SINT)
765 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200766 }
767
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200768 if (!htx)
769 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200770
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200771 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
772 /* search for header from the beginning */
773 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200774
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200775 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
776 /* no explicit occurrence and single fetch => last header by default */
777 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200778
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200779 if (!occ)
780 /* prepare to report multiple occurrences for ACL fetches */
781 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200782
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200783 smp->data.type = SMP_T_STR;
784 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
785 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
786 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200787 smp->flags &= ~SMP_F_NOT_LAST;
788 return 0;
789}
790
791/* 6. Check on HTTP header count. The number of occurrences is returned.
792 * Accepts exactly 1 argument of type string. It does not stop on commas and
793 * returns full lines instead (useful for User-Agent or Date for example).
794 */
795static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
796{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200797 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
798 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200799 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200800 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200801 struct http_hdr_ctx ctx;
802 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200803 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200804
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200805 if (!htx)
806 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200807
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200808 if (args && args->type == ARGT_STR) {
809 name.ptr = args->data.str.area;
810 name.len = args->data.str.data;
811 } else {
812 name.ptr = NULL;
813 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200814 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200815
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200816 ctx.blk = NULL;
817 cnt = 0;
818 while (http_find_header(htx, name, &ctx, 1))
819 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200820 smp->data.type = SMP_T_SINT;
821 smp->data.u.sint = cnt;
822 smp->flags = SMP_F_VOL_HDR;
823 return 1;
824}
825
826static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
827{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200828 /* possible keywords: req.hdr_names, res.hdr_names */
829 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200830 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200831 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200832 struct buffer *temp;
833 char del = ',';
834
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200835 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200836
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200837 if (!htx)
838 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200839
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200840 if (args && args->type == ARGT_STR)
841 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200842
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200843 temp = get_trash_chunk();
844 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
845 struct htx_blk *blk = htx_get_blk(htx, pos);
846 enum htx_blk_type type = htx_get_blk_type(blk);
847 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200848
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200849 if (type == HTX_BLK_EOH)
850 break;
851 if (type != HTX_BLK_HDR)
852 continue;
853 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200854
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200855 if (temp->data)
856 temp->area[temp->data++] = del;
857 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200858 }
859
860 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200861 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200862 smp->flags = SMP_F_VOL_HDR;
863 return 1;
864}
865
866/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
867 * Accepts an optional argument of type string containing the header field name,
868 * and an optional argument of type signed or unsigned integer to request an
869 * explicit occurrence of the header. Note that in the event of a missing name,
870 * headers are considered from the first one.
871 */
872static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
873{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200874 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
875 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200876 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200877 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200878 struct http_hdr_ctx *ctx = smp->ctx.a[0];
879 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200880 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200881
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200882 if (!ctx) {
883 /* first call */
884 ctx = &static_http_hdr_ctx;
885 ctx->blk = NULL;
886 smp->ctx.a[0] = ctx;
887 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200888
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200889 if (args) {
890 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200891 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200892 name.ptr = args[0].data.str.area;
893 name.len = args[0].data.str.data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200894
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200895 if (args[1].type == ARGT_SINT)
896 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200897 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200898
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200899 if (!htx)
900 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200901
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200902 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
903 /* search for header from the beginning */
904 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200905
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200906 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
907 /* no explicit occurrence and single fetch => last header by default */
908 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200909
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200910 if (!occ)
911 /* prepare to report multiple occurrences for ACL fetches */
912 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200913
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200914 smp->data.type = SMP_T_STR;
915 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
916 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
917 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200918
919 smp->flags &= ~SMP_F_NOT_LAST;
920 return 0;
921}
922
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200923/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
924 * the right channel. So instead of duplicating the code, we just change the
925 * keyword and then fallback on smp_fetch_hdr().
926 */
927static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
928{
929 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
930 return smp_fetch_hdr(args, smp, kw, private);
931}
932
Willy Tarreau79e57332018-10-02 16:01:16 +0200933/* 6. Check on HTTP header count. The number of occurrences is returned.
934 * Accepts exactly 1 argument of type string.
935 */
936static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
937{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200938 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
939 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200940 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200941 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200942 struct http_hdr_ctx ctx;
943 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200944 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200945
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200946 if (!htx)
947 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200948
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200949 if (args && args->type == ARGT_STR) {
950 name.ptr = args->data.str.area;
951 name.len = args->data.str.data;
952 } else {
953 name.ptr = NULL;
954 name.len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200955 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200956
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200957 ctx.blk = NULL;
958 cnt = 0;
959 while (http_find_header(htx, name, &ctx, 0))
960 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200961
962 smp->data.type = SMP_T_SINT;
963 smp->data.u.sint = cnt;
964 smp->flags = SMP_F_VOL_HDR;
965 return 1;
966}
967
968/* Fetch an HTTP header's integer value. The integer value is returned. It
969 * takes a mandatory argument of type string and an optional one of type int
970 * to designate a specific occurrence. It returns an unsigned integer, which
971 * may or may not be appropriate for everything.
972 */
973static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
974{
975 int ret = smp_fetch_hdr(args, smp, kw, private);
976
977 if (ret > 0) {
978 smp->data.type = SMP_T_SINT;
979 smp->data.u.sint = strl2ic(smp->data.u.str.area,
980 smp->data.u.str.data);
981 }
982
983 return ret;
984}
985
986/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
987 * and an optional one of type int to designate a specific occurrence.
988 * It returns an IPv4 or IPv6 address.
989 */
990static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
991{
992 int ret;
993
994 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
995 if (url2ipv4((char *) smp->data.u.str.area, &smp->data.u.ipv4)) {
996 smp->data.type = SMP_T_IPV4;
997 break;
998 } else {
999 struct buffer *temp = get_trash_chunk();
1000 if (smp->data.u.str.data < temp->size - 1) {
1001 memcpy(temp->area, smp->data.u.str.area,
1002 smp->data.u.str.data);
1003 temp->area[smp->data.u.str.data] = '\0';
1004 if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1005 smp->data.type = SMP_T_IPV6;
1006 break;
1007 }
1008 }
1009 }
1010
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001011 /* if the header doesn't match an IP address, fetch next one */
1012 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001013 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001014 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001015 return ret;
1016}
Willy Tarreau79e57332018-10-02 16:01:16 +02001017
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001018/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
1019 * the first '/' after the possible hostname, and ends before the possible '?'.
1020 */
1021static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1022{
1023 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001024 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001025 struct htx_sl *sl;
1026 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001027
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001028 if (!htx)
1029 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001030
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001031 sl = http_get_stline(htx);
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001032 path = iststop(http_get_path(htx_sl_req_uri(sl)), '?');
Tim Duesterhused526372020-03-05 17:56:33 +01001033 if (!isttest(path))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001034 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001035
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001036 /* OK, we got the '/' ! */
1037 smp->data.type = SMP_T_STR;
1038 smp->data.u.str.area = path.ptr;
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001039 smp->data.u.str.data = path.len;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001040 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001041 return 1;
1042}
1043
1044/* This produces a concatenation of the first occurrence of the Host header
1045 * followed by the path component if it begins with a slash ('/'). This means
1046 * that '*' will not be added, resulting in exactly the first Host entry.
1047 * If no Host header is found, then the path is returned as-is. The returned
1048 * value is stored in the trash so it does not need to be marked constant.
1049 * The returned sample is of type string.
1050 */
1051static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1052{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001053 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001054 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001055 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001056 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001057 struct http_hdr_ctx ctx;
1058 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001059
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001060 if (!htx)
1061 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001062
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001063 ctx.blk = NULL;
1064 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1065 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001066
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001067 /* OK we have the header value in ctx.value */
1068 temp = get_trash_chunk();
1069 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001070
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001071 /* now retrieve the path */
1072 sl = http_get_stline(htx);
1073 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001074 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001075 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001076
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001077 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1078 ;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001079
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001080 if (len && *(path.ptr) == '/')
1081 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001082 }
1083
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001084 smp->data.type = SMP_T_STR;
1085 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001086 smp->flags = SMP_F_VOL_1ST;
1087 return 1;
1088}
1089
1090/* This produces a 32-bit hash of the concatenation of the first occurrence of
1091 * the Host header followed by the path component if it begins with a slash ('/').
1092 * This means that '*' will not be added, resulting in exactly the first Host
1093 * entry. If no Host header is found, then the path is used. The resulting value
1094 * is hashed using the path hash followed by a full avalanche hash and provides a
1095 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1096 * high-traffic sites without having to store whole paths.
1097 */
1098static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1099{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001100 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001101 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001102 struct htx_sl *sl;
1103 struct http_hdr_ctx ctx;
1104 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001105 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001106
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001107 if (!htx)
1108 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001109
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001110 ctx.blk = NULL;
1111 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1112 /* OK we have the header value in ctx.value */
1113 while (ctx.value.len--)
1114 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001115 }
1116
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001117 /* now retrieve the path */
1118 sl = http_get_stline(htx);
1119 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001120 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001121 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001122
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001123 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1124 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001125
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001126 if (len && *(path.ptr) == '/') {
1127 while (len--)
1128 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001129 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001130 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001131
Willy Tarreau79e57332018-10-02 16:01:16 +02001132 hash = full_hash(hash);
1133
1134 smp->data.type = SMP_T_SINT;
1135 smp->data.u.sint = hash;
1136 smp->flags = SMP_F_VOL_1ST;
1137 return 1;
1138}
1139
1140/* This concatenates the source address with the 32-bit hash of the Host and
1141 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1142 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1143 * on the source address length. The path hash is stored before the address so
1144 * that in environments where IPv6 is insignificant, truncating the output to
1145 * 8 bytes would still work.
1146 */
1147static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1148{
1149 struct buffer *temp;
1150 struct connection *cli_conn = objt_conn(smp->sess->origin);
1151
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001152 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001153 return 0;
1154
1155 if (!smp_fetch_base32(args, smp, kw, private))
1156 return 0;
1157
1158 temp = get_trash_chunk();
1159 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1160 temp->data += sizeof(unsigned int);
1161
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001162 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001163 case AF_INET:
1164 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001165 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001166 4);
1167 temp->data += 4;
1168 break;
1169 case AF_INET6:
1170 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001171 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001172 16);
1173 temp->data += 16;
1174 break;
1175 default:
1176 return 0;
1177 }
1178
1179 smp->data.u.str = *temp;
1180 smp->data.type = SMP_T_BIN;
1181 return 1;
1182}
1183
1184/* Extracts the query string, which comes after the question mark '?'. If no
1185 * question mark is found, nothing is returned. Otherwise it returns a sample
1186 * of type string carrying the whole query string.
1187 */
1188static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1189{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001190 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001191 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001192 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001193 char *ptr, *end;
1194
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001195 if (!htx)
1196 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001197
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001198 sl = http_get_stline(htx);
1199 ptr = HTX_SL_REQ_UPTR(sl);
1200 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001201
1202 /* look up the '?' */
1203 do {
1204 if (ptr == end)
1205 return 0;
1206 } while (*ptr++ != '?');
1207
1208 smp->data.type = SMP_T_STR;
1209 smp->data.u.str.area = ptr;
1210 smp->data.u.str.data = end - ptr;
1211 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1212 return 1;
1213}
1214
1215static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1216{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001217 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001218 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001219
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001220 if (!htx)
1221 return 0;
1222 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001223 smp->data.u.sint = 1;
1224 return 1;
1225}
1226
1227/* return a valid test if the current request is the first one on the connection */
1228static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1229{
Willy Tarreau79512b62020-04-29 11:52:13 +02001230 if (!smp->strm)
1231 return 0;
1232
Willy Tarreau79e57332018-10-02 16:01:16 +02001233 smp->data.type = SMP_T_BOOL;
1234 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1235 return 1;
1236}
1237
Christopher Fauleta4063562019-08-02 11:51:37 +02001238/* Fetch the authentication method if there is an Authorization header. It
1239 * relies on get_http_auth()
1240 */
1241static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1242{
1243 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001244 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001245 struct http_txn *txn;
1246
1247 if (!htx)
1248 return 0;
1249
1250 txn = smp->strm->txn;
1251 if (!get_http_auth(smp, htx))
1252 return 0;
1253
1254 switch (txn->auth.method) {
1255 case HTTP_AUTH_BASIC:
1256 smp->data.u.str.area = "Basic";
1257 smp->data.u.str.data = 5;
1258 break;
1259 case HTTP_AUTH_DIGEST:
1260 /* Unexpected because not supported */
1261 smp->data.u.str.area = "Digest";
1262 smp->data.u.str.data = 6;
1263 break;
1264 default:
1265 return 0;
1266 }
1267
1268 smp->data.type = SMP_T_STR;
1269 smp->flags = SMP_F_CONST;
1270 return 1;
1271}
1272
1273/* Fetch the user supplied if there is an Authorization header. It relies on
1274 * get_http_auth()
1275 */
1276static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1277{
1278 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001279 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001280 struct http_txn *txn;
1281
1282 if (!htx)
1283 return 0;
1284
1285 txn = smp->strm->txn;
1286 if (!get_http_auth(smp, htx))
1287 return 0;
1288
1289 smp->data.type = SMP_T_STR;
1290 smp->data.u.str.area = txn->auth.user;
1291 smp->data.u.str.data = strlen(txn->auth.user);
1292 smp->flags = SMP_F_CONST;
1293 return 1;
1294}
1295
1296/* Fetch the password supplied if there is an Authorization header. It relies on
1297 * get_http_auth()
1298 */
1299static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1300{
1301 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001302 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001303 struct http_txn *txn;
1304
1305 if (!htx)
1306 return 0;
1307
1308 txn = smp->strm->txn;
1309 if (!get_http_auth(smp, htx))
1310 return 0;
1311
1312 smp->data.type = SMP_T_STR;
1313 smp->data.u.str.area = txn->auth.pass;
1314 smp->data.u.str.data = strlen(txn->auth.pass);
1315 smp->flags = SMP_F_CONST;
1316 return 1;
1317}
1318
Willy Tarreau79e57332018-10-02 16:01:16 +02001319/* Accepts exactly 1 argument of type userlist */
1320static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1321{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001322 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001323 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001324
1325 if (!args || args->type != ARGT_USR)
1326 return 0;
1327
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001328 if (!htx)
1329 return 0;
1330 if (!get_http_auth(smp, htx))
1331 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001332
1333 smp->data.type = SMP_T_BOOL;
1334 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001335 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001336 return 1;
1337}
1338
1339/* Accepts exactly 1 argument of type userlist */
1340static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1341{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001342 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001343 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001344
Willy Tarreau79e57332018-10-02 16:01:16 +02001345 if (!args || args->type != ARGT_USR)
1346 return 0;
1347
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001348 if (!htx)
1349 return 0;
1350 if (!get_http_auth(smp, htx))
1351 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001352
Willy Tarreau79e57332018-10-02 16:01:16 +02001353 /* if the user does not belong to the userlist or has a wrong password,
1354 * report that it unconditionally does not match. Otherwise we return
1355 * a string containing the username.
1356 */
1357 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1358 smp->strm->txn->auth.pass))
1359 return 0;
1360
1361 /* pat_match_auth() will need the user list */
1362 smp->ctx.a[0] = args->data.usr;
1363
1364 smp->data.type = SMP_T_STR;
1365 smp->flags = SMP_F_CONST;
1366 smp->data.u.str.area = smp->strm->txn->auth.user;
1367 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1368
1369 return 1;
1370}
1371
1372/* Fetch a captured HTTP request header. The index is the position of
1373 * the "capture" option in the configuration file
1374 */
1375static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1376{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001377 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001378 int idx;
1379
1380 if (!args || args->type != ARGT_SINT)
1381 return 0;
1382
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001383 if (!smp->strm)
1384 return 0;
1385
1386 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001387 idx = args->data.sint;
1388
1389 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1390 return 0;
1391
1392 smp->data.type = SMP_T_STR;
1393 smp->flags |= SMP_F_CONST;
1394 smp->data.u.str.area = smp->strm->req_cap[idx];
1395 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1396
1397 return 1;
1398}
1399
1400/* Fetch a captured HTTP response header. The index is the position of
1401 * the "capture" option in the configuration file
1402 */
1403static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1404{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001405 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001406 int idx;
1407
1408 if (!args || args->type != ARGT_SINT)
1409 return 0;
1410
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001411 if (!smp->strm)
1412 return 0;
1413
1414 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001415 idx = args->data.sint;
1416
1417 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1418 return 0;
1419
1420 smp->data.type = SMP_T_STR;
1421 smp->flags |= SMP_F_CONST;
1422 smp->data.u.str.area = smp->strm->res_cap[idx];
1423 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1424
1425 return 1;
1426}
1427
1428/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1429static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1430{
1431 struct buffer *temp;
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001432 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001433 char *ptr;
1434
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001435 if (!smp->strm)
1436 return 0;
1437
1438 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001439 if (!txn || !txn->uri)
1440 return 0;
1441
1442 ptr = txn->uri;
1443
1444 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1445 ptr++;
1446
1447 temp = get_trash_chunk();
1448 temp->area = txn->uri;
1449 temp->data = ptr - txn->uri;
1450 smp->data.u.str = *temp;
1451 smp->data.type = SMP_T_STR;
1452 smp->flags = SMP_F_CONST;
1453
1454 return 1;
1455
1456}
1457
1458/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1459static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1460{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001461 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001462 struct ist path;
1463 const char *ptr;
1464
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001465 if (!smp->strm)
1466 return 0;
1467
1468 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001469 if (!txn || !txn->uri)
1470 return 0;
1471
1472 ptr = txn->uri;
1473
1474 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1475 ptr++;
1476
1477 if (!*ptr)
1478 return 0;
1479
Christopher Faulet78337bb2018-11-15 14:35:18 +01001480 /* skip the first space and find space after URI */
1481 path = ist2(++ptr, 0);
1482 while (*ptr != ' ' && *ptr != '\0')
1483 ptr++;
1484 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001485
Christopher Faulet78337bb2018-11-15 14:35:18 +01001486 path = http_get_path(path);
Tim Duesterhused526372020-03-05 17:56:33 +01001487 if (!isttest(path))
Willy Tarreau79e57332018-10-02 16:01:16 +02001488 return 0;
1489
1490 smp->data.u.str.area = path.ptr;
1491 smp->data.u.str.data = path.len;
1492 smp->data.type = SMP_T_STR;
1493 smp->flags = SMP_F_CONST;
1494
1495 return 1;
1496}
1497
1498/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1499 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1500 */
1501static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1502{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001503 struct http_txn *txn;
1504
1505 if (!smp->strm)
1506 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001507
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001508 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001509 if (!txn || txn->req.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001510 return 0;
1511
1512 if (txn->req.flags & HTTP_MSGF_VER_11)
1513 smp->data.u.str.area = "HTTP/1.1";
1514 else
1515 smp->data.u.str.area = "HTTP/1.0";
1516
1517 smp->data.u.str.data = 8;
1518 smp->data.type = SMP_T_STR;
1519 smp->flags = SMP_F_CONST;
1520 return 1;
1521
1522}
1523
1524/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1525 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1526 */
1527static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1528{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001529 struct http_txn *txn;
1530
1531 if (!smp->strm)
1532 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001533
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001534 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001535 if (!txn || txn->rsp.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001536 return 0;
1537
1538 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1539 smp->data.u.str.area = "HTTP/1.1";
1540 else
1541 smp->data.u.str.area = "HTTP/1.0";
1542
1543 smp->data.u.str.data = 8;
1544 smp->data.type = SMP_T_STR;
1545 smp->flags = SMP_F_CONST;
1546 return 1;
1547
1548}
1549
1550/* Iterate over all cookies present in a message. The context is stored in
1551 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1552 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1553 * the direction, multiple cookies may be parsed on the same line or not.
1554 * The cookie name is in args and the name length in args->data.str.len.
1555 * Accepts exactly 1 argument of type string. If the input options indicate
1556 * that no iterating is desired, then only last value is fetched if any.
1557 * The returned sample is of type CSTR. Can be used to parse cookies in other
1558 * files.
1559 */
1560static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1561{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001562 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1563 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001564 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001565 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001566 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1567 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001568 int occ = 0;
1569 int found = 0;
1570
1571 if (!args || args->type != ARGT_STR)
1572 return 0;
1573
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001574 if (!ctx) {
1575 /* first call */
1576 ctx = &static_http_hdr_ctx;
1577 ctx->blk = NULL;
1578 smp->ctx.a[2] = ctx;
1579 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001580
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001581 if (!htx)
1582 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001583
Christopher Faulet16032ab2020-04-30 11:30:00 +02001584 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001585
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001586 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1587 /* no explicit occurrence and single fetch => last cookie by default */
1588 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001589
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001590 /* OK so basically here, either we want only one value and it's the
1591 * last one, or we want to iterate over all of them and we fetch the
1592 * next one.
1593 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001594
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001595 if (!(smp->flags & SMP_F_NOT_LAST)) {
1596 /* search for the header from the beginning, we must first initialize
1597 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001598 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001599 smp->ctx.a[0] = NULL;
1600 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001601 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001602
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001603 smp->flags |= SMP_F_VOL_HDR;
1604 while (1) {
1605 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1606 if (!smp->ctx.a[0]) {
1607 if (!http_find_header(htx, hdr, ctx, 0))
1608 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001609
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001610 if (ctx->value.len < args->data.str.data + 1)
1611 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001612
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001613 smp->ctx.a[0] = ctx->value.ptr;
1614 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001615 }
1616
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001617 smp->data.type = SMP_T_STR;
1618 smp->flags |= SMP_F_CONST;
1619 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
1620 args->data.str.area, args->data.str.data,
1621 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1622 &smp->data.u.str.area,
1623 &smp->data.u.str.data);
1624 if (smp->ctx.a[0]) {
1625 found = 1;
1626 if (occ >= 0) {
1627 /* one value was returned into smp->data.u.str.{str,len} */
1628 smp->flags |= SMP_F_NOT_LAST;
1629 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001630 }
1631 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001632 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001633 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001634
Willy Tarreau79e57332018-10-02 16:01:16 +02001635 /* all cookie headers and values were scanned. If we're looking for the
1636 * last occurrence, we may return it now.
1637 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001638 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001639 smp->flags &= ~SMP_F_NOT_LAST;
1640 return found;
1641}
1642
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001643/* Same than smp_fetch_cookie() but only relies on the sample direction to
1644 * choose the right channel. So instead of duplicating the code, we just change
1645 * the keyword and then fallback on smp_fetch_cookie().
1646 */
1647static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1648{
1649 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1650 return smp_fetch_cookie(args, smp, kw, private);
1651}
1652
Willy Tarreau79e57332018-10-02 16:01:16 +02001653/* Iterate over all cookies present in a request to count how many occurrences
1654 * match the name in args and args->data.str.len. If <multi> is non-null, then
1655 * multiple cookies may be parsed on the same line. The returned sample is of
1656 * type UINT. Accepts exactly 1 argument of type string.
1657 */
1658static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1659{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001660 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1661 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001662 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001663 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001664 struct http_hdr_ctx ctx;
1665 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001666 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001667 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001668
1669 if (!args || args->type != ARGT_STR)
1670 return 0;
1671
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001672 if (!htx)
1673 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001674
Christopher Faulet16032ab2020-04-30 11:30:00 +02001675 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001676
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001677 val_end = val_beg = NULL;
1678 ctx.blk = NULL;
1679 cnt = 0;
1680 while (1) {
1681 /* Note: val_beg == NULL every time we need to fetch a new header */
1682 if (!val_beg) {
1683 if (!http_find_header(htx, hdr, &ctx, 0))
1684 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001685
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001686 if (ctx.value.len < args->data.str.data + 1)
1687 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001688
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001689 val_beg = ctx.value.ptr;
1690 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001691 }
1692
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001693 smp->data.type = SMP_T_STR;
1694 smp->flags |= SMP_F_CONST;
1695 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
1696 args->data.str.area, args->data.str.data,
1697 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1698 &smp->data.u.str.area,
1699 &smp->data.u.str.data))) {
1700 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001701 }
1702 }
1703
1704 smp->data.type = SMP_T_SINT;
1705 smp->data.u.sint = cnt;
1706 smp->flags |= SMP_F_VOL_HDR;
1707 return 1;
1708}
1709
1710/* Fetch an cookie's integer value. The integer value is returned. It
1711 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1712 */
1713static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1714{
1715 int ret = smp_fetch_cookie(args, smp, kw, private);
1716
1717 if (ret > 0) {
1718 smp->data.type = SMP_T_SINT;
1719 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1720 smp->data.u.str.data);
1721 }
1722
1723 return ret;
1724}
1725
1726/************************************************************************/
1727/* The code below is dedicated to sample fetches */
1728/************************************************************************/
1729
1730/* This scans a URL-encoded query string. It takes an optionally wrapping
1731 * string whose first contigous chunk has its beginning in ctx->a[0] and end
1732 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1733 * pointers are updated for next iteration before leaving.
1734 */
1735static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1736{
1737 const char *vstart, *vend;
1738 struct buffer *temp;
1739 const char **chunks = (const char **)smp->ctx.a;
1740
1741 if (!http_find_next_url_param(chunks, name, name_len,
1742 &vstart, &vend, delim))
1743 return 0;
1744
1745 /* Create sample. If the value is contiguous, return the pointer as CONST,
1746 * if the value is wrapped, copy-it in a buffer.
1747 */
1748 smp->data.type = SMP_T_STR;
1749 if (chunks[2] &&
1750 vstart >= chunks[0] && vstart <= chunks[1] &&
1751 vend >= chunks[2] && vend <= chunks[3]) {
1752 /* Wrapped case. */
1753 temp = get_trash_chunk();
1754 memcpy(temp->area, vstart, chunks[1] - vstart);
1755 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1756 vend - chunks[2]);
1757 smp->data.u.str.area = temp->area;
1758 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1759 } else {
1760 /* Contiguous case. */
1761 smp->data.u.str.area = (char *)vstart;
1762 smp->data.u.str.data = vend - vstart;
1763 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1764 }
1765
1766 /* Update context, check wrapping. */
1767 chunks[0] = vend;
1768 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1769 chunks[1] = chunks[3];
1770 chunks[2] = NULL;
1771 }
1772
1773 if (chunks[0] < chunks[1])
1774 smp->flags |= SMP_F_NOT_LAST;
1775
1776 return 1;
1777}
1778
1779/* This function iterates over each parameter of the query string. It uses
1780 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1781 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1782 * An optional parameter name is passed in args[0], otherwise any parameter is
1783 * considered. It supports an optional delimiter argument for the beginning of
1784 * the string in args[1], which defaults to "?".
1785 */
1786static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1787{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001788 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001789 char delim = '?';
1790 const char *name;
1791 int name_len;
1792
1793 if (!args ||
1794 (args[0].type && args[0].type != ARGT_STR) ||
1795 (args[1].type && args[1].type != ARGT_STR))
1796 return 0;
1797
1798 name = "";
1799 name_len = 0;
1800 if (args->type == ARGT_STR) {
1801 name = args->data.str.area;
1802 name_len = args->data.str.data;
1803 }
1804
1805 if (args[1].type)
1806 delim = *args[1].data.str.area;
1807
1808 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001809 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001810 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001811
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001812 if (!htx)
1813 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001814
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001815 sl = http_get_stline(htx);
1816 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1817 if (!smp->ctx.a[0])
1818 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001819
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001820 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001821
1822 /* Assume that the context is filled with NULL pointer
1823 * before the first call.
1824 * smp->ctx.a[2] = NULL;
1825 * smp->ctx.a[3] = NULL;
1826 */
1827 }
1828
1829 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1830}
1831
1832/* This function iterates over each parameter of the body. This requires
1833 * that the body has been waited for using http-buffer-request. It uses
1834 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
1835 * contigous part of the body, and optionally ctx->a[2..3] to reference the
1836 * optional second part if the body wraps at the end of the buffer. An optional
1837 * parameter name is passed in args[0], otherwise any parameter is considered.
1838 */
1839static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1840{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001841 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001842 const char *name;
1843 int name_len;
1844
1845 if (!args || (args[0].type && args[0].type != ARGT_STR))
1846 return 0;
1847
1848 name = "";
1849 name_len = 0;
1850 if (args[0].type == ARGT_STR) {
1851 name = args[0].data.str.area;
1852 name_len = args[0].data.str.data;
1853 }
1854
1855 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulete596d182020-05-05 17:46:34 +02001856 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001857 struct buffer *temp;
1858 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001859
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001860 if (!htx)
1861 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001862
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001863 temp = get_trash_chunk();
1864 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1865 struct htx_blk *blk = htx_get_blk(htx, pos);
1866 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001867
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001868 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1869 break;
1870 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001871 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001872 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001873 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001874 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001875
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001876 smp->ctx.a[0] = temp->area;
1877 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001878
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001879 /* Assume that the context is filled with NULL pointer
1880 * before the first call.
1881 * smp->ctx.a[2] = NULL;
1882 * smp->ctx.a[3] = NULL;
1883 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001884
Willy Tarreau79e57332018-10-02 16:01:16 +02001885 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001886
Willy Tarreau79e57332018-10-02 16:01:16 +02001887 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1888}
1889
1890/* Return the signed integer value for the specified url parameter (see url_param
1891 * above).
1892 */
1893static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1894{
1895 int ret = smp_fetch_url_param(args, smp, kw, private);
1896
1897 if (ret > 0) {
1898 smp->data.type = SMP_T_SINT;
1899 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1900 smp->data.u.str.data);
1901 }
1902
1903 return ret;
1904}
1905
1906/* This produces a 32-bit hash of the concatenation of the first occurrence of
1907 * the Host header followed by the path component if it begins with a slash ('/').
1908 * This means that '*' will not be added, resulting in exactly the first Host
1909 * entry. If no Host header is found, then the path is used. The resulting value
1910 * is hashed using the url hash followed by a full avalanche hash and provides a
1911 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1912 * high-traffic sites without having to store whole paths.
1913 * this differs from the base32 functions in that it includes the url parameters
1914 * as well as the path
1915 */
1916static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1917{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001918 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001919 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001920 struct http_hdr_ctx ctx;
1921 struct htx_sl *sl;
1922 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001923 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001924
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001925 if (!htx)
1926 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001927
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001928 ctx.blk = NULL;
1929 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1930 /* OK we have the header value in ctx.value */
1931 while (ctx.value.len--)
1932 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001933 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001934
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001935 /* now retrieve the path */
1936 sl = http_get_stline(htx);
1937 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001938 if (path.len && *(path.ptr) == '/') {
1939 while (path.len--)
1940 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001941 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001942
Willy Tarreau79e57332018-10-02 16:01:16 +02001943 hash = full_hash(hash);
1944
1945 smp->data.type = SMP_T_SINT;
1946 smp->data.u.sint = hash;
1947 smp->flags = SMP_F_VOL_1ST;
1948 return 1;
1949}
1950
1951/* This concatenates the source address with the 32-bit hash of the Host and
1952 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1953 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1954 * on the source address length. The URL hash is stored before the address so
1955 * that in environments where IPv6 is insignificant, truncating the output to
1956 * 8 bytes would still work.
1957 */
1958static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1959{
1960 struct buffer *temp;
1961 struct connection *cli_conn = objt_conn(smp->sess->origin);
1962
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001963 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001964 return 0;
1965
1966 if (!smp_fetch_url32(args, smp, kw, private))
1967 return 0;
1968
1969 temp = get_trash_chunk();
1970 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1971 temp->data += sizeof(unsigned int);
1972
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001973 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001974 case AF_INET:
1975 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001976 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001977 4);
1978 temp->data += 4;
1979 break;
1980 case AF_INET6:
1981 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001982 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001983 16);
1984 temp->data += 16;
1985 break;
1986 default:
1987 return 0;
1988 }
1989
1990 smp->data.u.str = *temp;
1991 smp->data.type = SMP_T_BIN;
1992 return 1;
1993}
1994
1995/************************************************************************/
1996/* Other utility functions */
1997/************************************************************************/
1998
1999/* This function is used to validate the arguments passed to any "hdr" fetch
2000 * keyword. These keywords support an optional positive or negative occurrence
2001 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2002 * is assumed that the types are already the correct ones. Returns 0 on error,
2003 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2004 * error message in case of error, that the caller is responsible for freeing.
2005 * The initial location must either be freeable or NULL.
2006 * Note: this function's pointer is checked from Lua.
2007 */
2008int val_hdr(struct arg *arg, char **err_msg)
2009{
2010 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2011 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2012 return 0;
2013 }
2014 return 1;
2015}
2016
2017/************************************************************************/
2018/* All supported sample fetch keywords must be declared here. */
2019/************************************************************************/
2020
2021/* Note: must not be declared <const> as its list will be overwritten */
2022static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2023 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2024 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2025 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2026
2027 /* capture are allocated and are permanent in the stream */
2028 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2029
2030 /* retrieve these captures from the HTTP logs */
2031 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2032 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2033 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2034
2035 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2036 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2037
2038 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2039 * are only here to match the ACL's name, are request-only and are used
2040 * for ACL compatibility only.
2041 */
2042 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002043 { "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 +02002044 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2045 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2046
2047 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2048 * only here to match the ACL's name, are request-only and are used for
2049 * ACL compatibility only.
2050 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002051 { "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 +02002052 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2053 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2054 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2055
Christopher Fauleta4063562019-08-02 11:51:37 +02002056 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2057 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2058 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002059 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2060 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2061 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2062 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2063 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2064 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2065
2066 /* HTTP protocol on the request path */
2067 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2068 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2069
2070 /* HTTP version on the request path */
2071 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2072 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2073
2074 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2075 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2076 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2077 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2078
2079 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2080 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2081
2082 /* HTTP version on the response path */
2083 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2084 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2085
Christopher Faulete596d182020-05-05 17:46:34 +02002086 { "res.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2087 { "res.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2088 { "res.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2089
2090 { "res.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2091 { "res.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2092
Willy Tarreau79e57332018-10-02 16:01:16 +02002093 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2094 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2095 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2096 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2097
2098 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2099 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2100 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2101 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2102 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2103 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2104 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2105
2106 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2107 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2108 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2109 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2110
2111 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2112 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2113 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2114 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2115 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2116 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2117 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2118
2119 /* scook is valid only on the response and is used for ACL compatibility */
2120 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2121 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2122 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2123 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2124
2125 /* shdr is valid only on the response and is used for ACL compatibility */
2126 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2127 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2128 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2129 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2130
2131 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2132 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2133 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2134 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2135 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2136 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2137 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2138 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2139 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2140 { "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 +02002141
Willy Tarreau79e57332018-10-02 16:01:16 +02002142 { /* END */ },
2143}};
2144
Willy Tarreau0108d902018-11-25 19:14:37 +01002145INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002146
2147/*
2148 * Local variables:
2149 * c-indent-level: 8
2150 * c-basic-offset: 8
2151 * End:
2152 */