blob: 0a29c810aed2812dfbe6ff7fdc02b410f9b875df [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
Willy Tarreau17254932020-09-02 07:08:47 +0200116 p = memchr(ctx.value.ptr, ' ', ctx.value.len);
117 if (!p || p == ctx.value.ptr) /* if no space was found or if the space is the first character */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200118 return 0;
Willy Tarreau17254932020-09-02 07:08:47 +0200119 len = p - ctx.value.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +0200120
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200121 if (chunk_initlen(&auth_method, ctx.value.ptr, 0, len) != 1)
122 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200123
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200124 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.value.len - len - 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200125
126 if (!strncasecmp("Basic", auth_method.area, auth_method.data)) {
127 struct buffer *http_auth = get_trash_chunk();
128
129 len = base64dec(txn->auth.method_data.area,
130 txn->auth.method_data.data,
131 http_auth->area, global.tune.bufsize - 1);
132
133 if (len < 0)
134 return 0;
135
136
137 http_auth->area[len] = '\0';
138
139 p = strchr(http_auth->area, ':');
140
141 if (!p)
142 return 0;
143
144 txn->auth.user = http_auth->area;
145 *p = '\0';
146 txn->auth.pass = p+1;
147
148 txn->auth.method = HTTP_AUTH_BASIC;
149 return 1;
150 }
151
152 return 0;
153}
154
155/* This function ensures that the prerequisites for an L7 fetch are ready,
156 * which means that a request or response is ready. If some data is missing,
157 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200158 * to extract data from L7. If <vol> is non-null during a prefetch, another
159 * test is made to ensure the required information is not gone.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200160 *
161 * The function returns :
162 * NULL with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
163 * decide whether or not an HTTP message is present ;
164 * NULL if the requested data cannot be fetched or if it is certain that
Willy Tarreaueae83722020-04-29 11:52:51 +0200165 * we'll never have any HTTP message there; this includes null strm or chn.
Willy Tarreaua6d98792020-08-12 14:04:52 +0200166 * NULL if the sample's direction does not match the channel's (i.e. the
167 * function was asked to work on the wrong channel)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200168 * The HTX message if ready
169 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200170struct htx *smp_prefetch_htx(struct sample *smp, struct channel *chn, struct check *check, int vol)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200171{
Christopher Fauletef453ed2018-10-24 21:39:27 +0200172 struct stream *s = smp->strm;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200173 struct http_txn *txn = NULL;
174 struct htx *htx = NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200175 struct http_msg *msg;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100176 struct htx_sl *sl;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200177
Willy Tarreaua6d98792020-08-12 14:04:52 +0200178 if (chn &&
179 (((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ && (chn->flags & CF_ISRESP)) ||
180 ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES && !(chn->flags & CF_ISRESP))))
181 return 0;
182
Christopher Fauletef453ed2018-10-24 21:39:27 +0200183 /* Note: it is possible that <s> is NULL when called before stream
184 * initialization (eg: tcp-request connection), so this function is the
185 * one responsible for guarding against this case for all HTTP users.
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200186 *
187 * In the health check context, the stream and the channel must be NULL
188 * and <check> must be set. In this case, only the input buffer,
189 * corresponding to the response, is considered. It is the caller
190 * responsibility to provide <check>.
Christopher Fauletef453ed2018-10-24 21:39:27 +0200191 */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200192 BUG_ON(check && (s || chn));
193 if (!s || !chn) {
194 if (check) {
195 htx = htxbuf(&check->bi);
196
197 /* Analyse not yet started */
198 if (htx_is_empty(htx) || htx->first == -1)
199 return NULL;
200
201 sl = http_get_stline(htx);
202 if (vol && !sl) {
203 /* The start-line was already forwarded, it is too late to fetch anything */
204 return NULL;
205 }
206 goto end;
207 }
208
Christopher Fauletef453ed2018-10-24 21:39:27 +0200209 return NULL;
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200210 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200211
212 if (!s->txn) {
213 if (unlikely(!http_alloc_txn(s)))
214 return NULL; /* not enough memory */
215 http_init_txn(s);
216 txn = s->txn;
217 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200218 txn = s->txn;
219 msg = (!(chn->flags & CF_ISRESP) ? &txn->req : &txn->rsp);
220 smp->data.type = SMP_T_BOOL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200221
Christopher Fauleteca88542019-04-03 10:12:42 +0200222 if (IS_HTX_STRM(s)) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200223 htx = htxbuf(&chn->buf);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200224
Christopher Faulet89dc4992019-04-17 12:02:59 +0200225 if (msg->msg_state == HTTP_MSG_ERROR || (htx->flags & HTX_FL_PARSING_ERROR))
226 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200227
Christopher Faulet89dc4992019-04-17 12:02:59 +0200228 if (msg->msg_state < HTTP_MSG_BODY) {
229 /* Analyse not yet started */
Christopher Faulet29f17582019-05-23 11:03:26 +0200230 if (htx_is_empty(htx) || htx->first == -1) {
Christopher Fauletef453ed2018-10-24 21:39:27 +0200231 /* Parsing is done by the mux, just wait */
232 smp->flags |= SMP_F_MAY_CHANGE;
233 return NULL;
234 }
235 }
Christopher Faulet297fbb42019-05-13 14:41:27 +0200236 sl = http_get_stline(htx);
Christopher Faulet5ec8bcb2019-04-17 12:04:12 +0200237 if (vol && !sl) {
Christopher Faulet89dc4992019-04-17 12:02:59 +0200238 /* The start-line was already forwarded, it is too late to fetch anything */
239 return NULL;
240 }
Christopher Fauletef453ed2018-10-24 21:39:27 +0200241 }
Christopher Fauleteca88542019-04-03 10:12:42 +0200242 else { /* RAW mode */
Christopher Faulet89dc4992019-04-17 12:02:59 +0200243 struct buffer *buf;
244 struct h1m h1m;
Christopher Faulete4ab11b2019-06-11 15:05:37 +0200245 struct http_hdr hdrs[global.tune.max_http_hdr];
Christopher Faulet89dc4992019-04-17 12:02:59 +0200246 union h1_sl h1sl;
247 unsigned int flags = HTX_FL_NONE;
248 int ret;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200249
Christopher Faulet89dc4992019-04-17 12:02:59 +0200250 /* no HTTP fetch on the response in TCP mode */
251 if (chn->flags & CF_ISRESP)
252 return NULL;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200253
Christopher Faulet89dc4992019-04-17 12:02:59 +0200254 /* Now we are working on the request only */
255 buf = &chn->buf;
256 if (b_head(buf) + b_data(buf) > b_wrap(buf))
257 b_slow_realign(buf, trash.area, 0);
Christopher Fauletef453ed2018-10-24 21:39:27 +0200258
Christopher Faulet89dc4992019-04-17 12:02:59 +0200259 h1m_init_req(&h1m);
260 ret = h1_headers_to_hdr_list(b_head(buf), b_stop(buf),
261 hdrs, sizeof(hdrs)/sizeof(hdrs[0]), &h1m, &h1sl);
262 if (ret <= 0) {
263 /* Invalid or too big*/
264 if (ret < 0 || channel_full(&s->req, global.tune.maxrewrite))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200265 return NULL;
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100266
Christopher Faulet89dc4992019-04-17 12:02:59 +0200267 /* wait for a full request */
268 smp->flags |= SMP_F_MAY_CHANGE;
269 return NULL;
270 }
Christopher Fauletf1ba18d2018-11-26 21:37:08 +0100271
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500272 /* OK we just got a valid HTTP message. We have to convert it
Christopher Faulet89dc4992019-04-17 12:02:59 +0200273 * into an HTX message.
274 */
275 if (unlikely(h1sl.rq.v.len == 0)) {
276 /* try to convert HTTP/0.9 requests to HTTP/1.0 */
277 if (h1sl.rq.meth != HTTP_METH_GET || !h1sl.rq.u.len)
Christopher Fauletef453ed2018-10-24 21:39:27 +0200278 return NULL;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200279 h1sl.rq.v = ist("HTTP/1.0");
Christopher Fauletef453ed2018-10-24 21:39:27 +0200280 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200281
282 /* Set HTX start-line flags */
283 if (h1m.flags & H1_MF_VER_11)
284 flags |= HTX_SL_F_VER_11;
285 if (h1m.flags & H1_MF_XFER_ENC)
286 flags |= HTX_SL_F_XFER_ENC;
287 flags |= HTX_SL_F_XFER_LEN;
288 if (h1m.flags & H1_MF_CHNK)
289 flags |= HTX_SL_F_CHNK;
290 else if (h1m.flags & H1_MF_CLEN)
291 flags |= HTX_SL_F_CLEN;
292
Richard Russo458eafb2019-07-31 11:45:56 -0700293 htx = htx_from_buf(get_raw_htx_chunk());
Christopher Faulet89dc4992019-04-17 12:02:59 +0200294 sl = htx_add_stline(htx, HTX_BLK_REQ_SL, flags, h1sl.rq.m, h1sl.rq.u, h1sl.rq.v);
295 if (!sl || !htx_add_all_headers(htx, hdrs))
Christopher Fauletef453ed2018-10-24 21:39:27 +0200296 return NULL;
Willy Tarreauce9bbf52019-05-13 08:32:31 +0200297 sl->info.req.meth = h1sl.rq.meth;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200298 }
299
300 /* OK we just got a valid HTTP message. If not already done by
301 * HTTP analyzers, we have some minor preparation to perform so
302 * that further checks can rely on HTTP tests.
303 */
304 if (sl && msg->msg_state < HTTP_MSG_BODY) {
305 if (!(chn->flags & CF_ISRESP)) {
306 txn->meth = sl->info.req.meth;
307 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
308 s->flags |= SF_REDIRECTABLE;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200309 }
Christopher Faulet89dc4992019-04-17 12:02:59 +0200310 else
311 txn->status = sl->info.res.status;
312 if (sl->flags & HTX_SL_F_VER_11)
313 msg->flags |= HTTP_MSGF_VER_11;
Christopher Fauletef453ed2018-10-24 21:39:27 +0200314 }
315
316 /* everything's OK */
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200317 end:
Christopher Fauletef453ed2018-10-24 21:39:27 +0200318 smp->data.u.sint = 1;
319 return htx;
320}
321
Willy Tarreau79e57332018-10-02 16:01:16 +0200322/* This function fetches the method of current HTTP request and stores
323 * it in the global pattern struct as a chunk. There are two possibilities :
324 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
325 * in <len> and <ptr> is NULL ;
326 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
327 * <len> to its length.
328 * This is intended to be used with pat_match_meth() only.
329 */
330static int smp_fetch_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
331{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200332 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +0200333 struct http_txn *txn;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200334 int meth;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200335
Willy Tarreaua6d98792020-08-12 14:04:52 +0200336 txn = smp->strm->txn;
337 if (!txn)
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200338 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200339
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200340 meth = txn->meth;
341 smp->data.type = SMP_T_METH;
342 smp->data.u.meth.meth = meth;
343 if (meth == HTTP_METH_OTHER) {
Willy Tarreaua6d98792020-08-12 14:04:52 +0200344 struct htx *htx;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200345 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200346
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200347 if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES) {
348 /* ensure the indexes are not affected */
349 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200350 }
Willy Tarreaua6d98792020-08-12 14:04:52 +0200351
352 htx = smp_prefetch_htx(smp, chn, NULL, 0);
353 if (!htx)
354 return 0;
355
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200356 sl = http_get_stline(htx);
357 smp->flags |= SMP_F_CONST;
358 smp->data.u.meth.str.area = HTX_SL_REQ_MPTR(sl);
359 smp->data.u.meth.str.data = HTX_SL_REQ_MLEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200360 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200361 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200362 return 1;
363}
364
365static int smp_fetch_rqver(const struct arg *args, struct sample *smp, const char *kw, void *private)
366{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200367 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200368 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200369 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200370 char *ptr;
371 int len;
372
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200373 if (!htx)
374 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200375
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200376 sl = http_get_stline(htx);
377 len = HTX_SL_REQ_VLEN(sl);
378 ptr = HTX_SL_REQ_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200379
380 while ((len-- > 0) && (*ptr++ != '/'));
381 if (len <= 0)
382 return 0;
383
384 smp->data.type = SMP_T_STR;
385 smp->data.u.str.area = ptr;
386 smp->data.u.str.data = len;
387
388 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
389 return 1;
390}
391
392static int smp_fetch_stver(const struct arg *args, struct sample *smp, const char *kw, void *private)
393{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200394 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200395 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200396 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200397 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200398 char *ptr;
399 int len;
400
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200401 if (!htx)
402 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200403
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200404 sl = http_get_stline(htx);
405 len = HTX_SL_RES_VLEN(sl);
406 ptr = HTX_SL_RES_VPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200407
408 while ((len-- > 0) && (*ptr++ != '/'));
409 if (len <= 0)
410 return 0;
411
412 smp->data.type = SMP_T_STR;
413 smp->data.u.str.area = ptr;
414 smp->data.u.str.data = len;
415
416 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
417 return 1;
418}
419
420/* 3. Check on Status Code. We manipulate integers here. */
421static int smp_fetch_stcode(const struct arg *args, struct sample *smp, const char *kw, void *private)
422{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200423 struct channel *chn = SMP_RES_CHN(smp);
Christopher Fauletf98e6262020-05-06 09:42:04 +0200424 struct check *check = objt_check(smp->sess->origin);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200425 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200426 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200427 char *ptr;
428 int len;
429
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200430 if (!htx)
431 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200432
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200433 sl = http_get_stline(htx);
434 len = HTX_SL_RES_CLEN(sl);
435 ptr = HTX_SL_RES_CPTR(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +0200436
437 smp->data.type = SMP_T_SINT;
438 smp->data.u.sint = __strl2ui(ptr, len);
439 smp->flags = SMP_F_VOL_1ST;
440 return 1;
441}
442
443static int smp_fetch_uniqueid(const struct arg *args, struct sample *smp, const char *kw, void *private)
444{
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100445 struct ist unique_id;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100446
Willy Tarreau79e57332018-10-02 16:01:16 +0200447 if (LIST_ISEMPTY(&smp->sess->fe->format_unique_id))
448 return 0;
449
Willy Tarreaua1062a42020-04-29 11:50:38 +0200450 if (!smp->strm)
451 return 0;
452
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100453 unique_id = stream_generate_unique_id(smp->strm, &smp->sess->fe->format_unique_id);
454 if (!isttest(unique_id))
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100455 return 0;
456
Tim Duesterhusa17e6622020-03-05 20:19:02 +0100457 smp->data.u.str.area = smp->strm->unique_id.ptr;
458 smp->data.u.str.data = smp->strm->unique_id.len;
Tim Duesterhus2825b4b2020-02-28 15:13:34 +0100459 smp->data.type = SMP_T_STR;
Willy Tarreau79e57332018-10-02 16:01:16 +0200460 smp->flags = SMP_F_CONST;
461 return 1;
462}
463
464/* Returns a string block containing all headers including the
Joseph Herlant942eea32018-11-15 13:57:22 -0800465 * empty line which separes headers from the body. This is useful
466 * for some headers analysis.
Willy Tarreau79e57332018-10-02 16:01:16 +0200467 */
468static int smp_fetch_hdrs(const struct arg *args, struct sample *smp, const char *kw, void *private)
469{
Christopher Faulete596d182020-05-05 17:46:34 +0200470 /* possible keywords: req.hdrs, res.hdrs */
471 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200472 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200473 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200474 struct buffer *temp;
475 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200476
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200477 if (!htx)
478 return 0;
479 temp = get_trash_chunk();
480 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
481 struct htx_blk *blk = htx_get_blk(htx, pos);
482 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200483
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200484 if (type == HTX_BLK_HDR) {
485 struct ist n = htx_get_blk_name(htx, blk);
486 struct ist v = htx_get_blk_value(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200487
Christopher Faulet53a899b2019-10-08 16:38:42 +0200488 if (!h1_format_htx_hdr(n, v, temp))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200489 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200490 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200491 else if (type == HTX_BLK_EOH) {
492 if (!chunk_memcat(temp, "\r\n", 2))
493 return 0;
494 break;
495 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200496 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200497 smp->data.type = SMP_T_STR;
498 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200499 return 1;
500}
501
502/* Returns the header request in a length/value encoded format.
503 * This is useful for exchanges with the SPOE.
504 *
505 * A "length value" is a multibyte code encoding numbers. It uses the
506 * SPOE format. The encoding is the following:
507 *
508 * Each couple "header name" / "header value" is composed
509 * like this:
510 * "length value" "header name bytes"
511 * "length value" "header value bytes"
512 * When the last header is reached, the header name and the header
513 * value are empty. Their length are 0
514 */
515static int smp_fetch_hdrs_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
516{
Christopher Faulete596d182020-05-05 17:46:34 +0200517 /* possible keywords: req.hdrs_bin, res.hdrs_bin */
518 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200519 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200520 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200521 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200522 char *p, *end;
523 int32_t pos;
524 int ret;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200525
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200526 if (!htx)
527 return 0;
528 temp = get_trash_chunk();
529 p = temp->area;
530 end = temp->area + temp->size;
531 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
532 struct htx_blk *blk = htx_get_blk(htx, pos);
533 enum htx_blk_type type = htx_get_blk_type(blk);
534 struct ist n, v;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200535
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200536 if (type == HTX_BLK_HDR) {
537 n = htx_get_blk_name(htx,blk);
538 v = htx_get_blk_value(htx, blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200539
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200540 /* encode the header name. */
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200541 ret = encode_varint(n.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200542 if (ret == -1)
543 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200544 if (p + n.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200545 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200546 memcpy(p, n.ptr, n.len);
547 p += n.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200548
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200549 /* encode the header value. */
550 ret = encode_varint(v.len, &p, end);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200551 if (ret == -1)
552 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200553 if (p + v.len > end)
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200554 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200555 memcpy(p, v.ptr, v.len);
556 p += v.len;
Willy Tarreau79e57332018-10-02 16:01:16 +0200557
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200558 }
559 else if (type == HTX_BLK_EOH) {
560 /* encode the end of the header list with empty
561 * header name and header value.
562 */
563 ret = encode_varint(0, &p, end);
564 if (ret == -1)
565 return 0;
566 ret = encode_varint(0, &p, end);
567 if (ret == -1)
568 return 0;
569 break;
570 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200571 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200572
573 /* Initialise sample data which will be filled. */
574 smp->data.type = SMP_T_BIN;
575 smp->data.u.str.area = temp->area;
576 smp->data.u.str.data = p - temp->area;
577 smp->data.u.str.size = temp->size;
Willy Tarreau79e57332018-10-02 16:01:16 +0200578 return 1;
579}
580
581/* returns the longest available part of the body. This requires that the body
582 * has been waited for using http-buffer-request.
583 */
584static int smp_fetch_body(const struct arg *args, struct sample *smp, const char *kw, void *private)
585{
Christopher Faulete596d182020-05-05 17:46:34 +0200586 /* possible keywords: req.body, res.body */
587 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200588 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200589 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200590 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200591 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +0200592
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200593 if (!htx)
594 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200595
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200596 temp = get_trash_chunk();
597 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
598 struct htx_blk *blk = htx_get_blk(htx, pos);
599 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +0200600
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200601 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
602 break;
603 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +0200604 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200605 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200606 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200607 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200608
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200609 smp->data.type = SMP_T_BIN;
610 smp->data.u.str = *temp;
611 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau9dc92b22020-06-15 18:01:10 +0200612
613 if (!channel_full(chn, global.tune.maxrewrite) && !(chn->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)))
614 smp->flags |= SMP_F_MAY_CHANGE;
615
Willy Tarreau79e57332018-10-02 16:01:16 +0200616 return 1;
617}
618
619
620/* returns the available length of the body. This requires that the body
621 * has been waited for using http-buffer-request.
622 */
623static int smp_fetch_body_len(const struct arg *args, struct sample *smp, const char *kw, void *private)
624{
Christopher Faulete596d182020-05-05 17:46:34 +0200625 /* possible keywords: req.body_len, res.body_len */
626 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200627 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200628 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200629 int32_t pos;
630 unsigned long long len = 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100631
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200632 if (!htx)
633 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100634
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200635 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
636 struct htx_blk *blk = htx_get_blk(htx, pos);
637 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100638
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200639 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
640 break;
641 if (type == HTX_BLK_DATA)
642 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200643 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200644
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200645 smp->data.type = SMP_T_SINT;
646 smp->data.u.sint = len;
647 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200648 return 1;
649}
650
651
652/* returns the advertised length of the body, or the advertised size of the
653 * chunks available in the buffer. This requires that the body has been waited
654 * for using http-buffer-request.
655 */
656static int smp_fetch_body_size(const struct arg *args, struct sample *smp, const char *kw, void *private)
657{
Christopher Faulete596d182020-05-05 17:46:34 +0200658 /* possible keywords: req.body_size, res.body_size */
659 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200660 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200661 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200662 int32_t pos;
663 unsigned long long len = 0;
Christopher Faulet89dc4992019-04-17 12:02:59 +0200664
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200665 if (!htx)
666 return 0;
Christopher Fauletc16317d2018-12-12 14:11:22 +0100667
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200668 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
669 struct htx_blk *blk = htx_get_blk(htx, pos);
670 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletc16317d2018-12-12 14:11:22 +0100671
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200672 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
673 break;
674 if (type == HTX_BLK_DATA)
675 len += htx_get_blksz(blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200676 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200677 if (htx->extra != ULLONG_MAX)
678 len += htx->extra;
Willy Tarreau79e57332018-10-02 16:01:16 +0200679
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200680 smp->data.type = SMP_T_SINT;
681 smp->data.u.sint = len;
682 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200683 return 1;
684}
685
686
687/* 4. Check on URL/URI. A pointer to the URI is stored. */
688static int smp_fetch_url(const struct arg *args, struct sample *smp, const char *kw, void *private)
689{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200690 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200691 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200692 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200693
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200694 if (!htx)
695 return 0;
696 sl = http_get_stline(htx);
697 smp->data.type = SMP_T_STR;
698 smp->data.u.str.area = HTX_SL_REQ_UPTR(sl);
699 smp->data.u.str.data = HTX_SL_REQ_ULEN(sl);
700 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +0200701 return 1;
702}
703
704static int smp_fetch_url_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
705{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200706 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200707 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200708 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200709 struct sockaddr_storage addr;
710
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200711 if (!htx)
712 return 0;
713 sl = http_get_stline(htx);
714 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Willy Tarreau79e57332018-10-02 16:01:16 +0200715
Willy Tarreau79e57332018-10-02 16:01:16 +0200716 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
717 return 0;
718
719 smp->data.type = SMP_T_IPV4;
720 smp->data.u.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
721 smp->flags = 0;
722 return 1;
723}
724
725static int smp_fetch_url_port(const struct arg *args, struct sample *smp, const char *kw, void *private)
726{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200727 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +0200728 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200729 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +0200730 struct sockaddr_storage addr;
731
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200732 if (!htx)
733 return 0;
734 sl = http_get_stline(htx);
735 url2sa(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), &addr, NULL);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200736
Willy Tarreau79e57332018-10-02 16:01:16 +0200737 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
738 return 0;
739
740 smp->data.type = SMP_T_SINT;
741 smp->data.u.sint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
742 smp->flags = 0;
743 return 1;
744}
745
746/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
747 * Accepts an optional argument of type string containing the header field name,
748 * and an optional argument of type signed or unsigned integer to request an
749 * explicit occurrence of the header. Note that in the event of a missing name,
750 * headers are considered from the first one. It does not stop on commas and
751 * returns full lines instead (useful for User-Agent or Date for example).
752 */
753static int smp_fetch_fhdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
754{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200755 /* possible keywords: req.fhdr, res.fhdr */
756 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200757 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200758 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200759 struct http_hdr_ctx *ctx = smp->ctx.a[0];
760 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200761 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200762
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200763 if (!ctx) {
764 /* first call */
765 ctx = &static_http_hdr_ctx;
766 ctx->blk = NULL;
767 smp->ctx.a[0] = ctx;
768 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200769
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200770 if (args) {
771 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200772 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200773 name.ptr = args[0].data.str.area;
774 name.len = args[0].data.str.data;
Willy Tarreau79e57332018-10-02 16:01:16 +0200775
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200776 if (args[1].type == ARGT_SINT)
777 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200778 }
779
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200780 if (!htx)
781 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200782
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200783 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
784 /* search for header from the beginning */
785 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200786
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200787 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
788 /* no explicit occurrence and single fetch => last header by default */
789 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200790
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200791 if (!occ)
792 /* prepare to report multiple occurrences for ACL fetches */
793 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200794
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200795 smp->data.type = SMP_T_STR;
796 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
797 if (http_get_htx_fhdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
798 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200799 smp->flags &= ~SMP_F_NOT_LAST;
800 return 0;
801}
802
803/* 6. Check on HTTP header count. The number of occurrences is returned.
804 * Accepts exactly 1 argument of type string. It does not stop on commas and
805 * returns full lines instead (useful for User-Agent or Date for example).
806 */
807static int smp_fetch_fhdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
808{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200809 /* possible keywords: req.fhdr_cnt, res.fhdr_cnt */
810 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200811 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200812 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200813 struct http_hdr_ctx ctx;
814 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200815 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200816
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200817 if (!htx)
818 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200819
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200820 if (args && args->type == ARGT_STR) {
821 name.ptr = args->data.str.area;
822 name.len = args->data.str.data;
823 } else {
824 name.ptr = NULL;
825 name.len = 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200826 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200827
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200828 ctx.blk = NULL;
829 cnt = 0;
830 while (http_find_header(htx, name, &ctx, 1))
831 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200832 smp->data.type = SMP_T_SINT;
833 smp->data.u.sint = cnt;
834 smp->flags = SMP_F_VOL_HDR;
835 return 1;
836}
837
838static int smp_fetch_hdr_names(const struct arg *args, struct sample *smp, const char *kw, void *private)
839{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200840 /* possible keywords: req.hdr_names, res.hdr_names */
841 struct channel *chn = ((kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200842 struct check *check = ((kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200843 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +0200844 struct buffer *temp;
845 char del = ',';
846
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200847 int32_t pos;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200848
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200849 if (!htx)
850 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200851
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200852 if (args && args->type == ARGT_STR)
853 del = *args[0].data.str.area;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200854
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200855 temp = get_trash_chunk();
856 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
857 struct htx_blk *blk = htx_get_blk(htx, pos);
858 enum htx_blk_type type = htx_get_blk_type(blk);
859 struct ist n;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200860
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200861 if (type == HTX_BLK_EOH)
862 break;
863 if (type != HTX_BLK_HDR)
864 continue;
865 n = htx_get_blk_name(htx, blk);
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200866
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200867 if (temp->data)
868 temp->area[temp->data++] = del;
869 chunk_memcat(temp, n.ptr, n.len);
Willy Tarreau79e57332018-10-02 16:01:16 +0200870 }
871
872 smp->data.type = SMP_T_STR;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200873 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +0200874 smp->flags = SMP_F_VOL_HDR;
875 return 1;
876}
877
878/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
879 * Accepts an optional argument of type string containing the header field name,
880 * and an optional argument of type signed or unsigned integer to request an
881 * explicit occurrence of the header. Note that in the event of a missing name,
882 * headers are considered from the first one.
883 */
884static int smp_fetch_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
885{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200886 /* possible keywords: req.hdr / hdr, res.hdr / shdr */
887 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200888 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200889 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200890 struct http_hdr_ctx *ctx = smp->ctx.a[0];
891 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200892 int occ = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200893
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200894 if (!ctx) {
895 /* first call */
896 ctx = &static_http_hdr_ctx;
897 ctx->blk = NULL;
898 smp->ctx.a[0] = ctx;
899 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200900
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200901 if (args) {
902 if (args[0].type != ARGT_STR)
Willy Tarreau79e57332018-10-02 16:01:16 +0200903 return 0;
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200904 name.ptr = args[0].data.str.area;
905 name.len = args[0].data.str.data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200906
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200907 if (args[1].type == ARGT_SINT)
908 occ = args[1].data.sint;
Willy Tarreau79e57332018-10-02 16:01:16 +0200909 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200910
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200911 if (!htx)
912 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200913
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200914 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
915 /* search for header from the beginning */
916 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +0200917
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200918 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
919 /* no explicit occurrence and single fetch => last header by default */
920 occ = -1;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200921
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200922 if (!occ)
923 /* prepare to report multiple occurrences for ACL fetches */
924 smp->flags |= SMP_F_NOT_LAST;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200925
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200926 smp->data.type = SMP_T_STR;
927 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
928 if (http_get_htx_hdr(htx, name, occ, ctx, &smp->data.u.str.area, &smp->data.u.str.data))
929 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +0200930
931 smp->flags &= ~SMP_F_NOT_LAST;
932 return 0;
933}
934
Christopher Fauletc1f40dd2019-05-16 10:07:30 +0200935/* Same than smp_fetch_hdr() but only relies on the sample direction to choose
936 * the right channel. So instead of duplicating the code, we just change the
937 * keyword and then fallback on smp_fetch_hdr().
938 */
939static int smp_fetch_chn_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
940{
941 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.hdr" : "res.hdr");
942 return smp_fetch_hdr(args, smp, kw, private);
943}
944
Willy Tarreau79e57332018-10-02 16:01:16 +0200945/* 6. Check on HTTP header count. The number of occurrences is returned.
946 * Accepts exactly 1 argument of type string.
947 */
948static int smp_fetch_hdr_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
949{
Christopher Faulet89dc4992019-04-17 12:02:59 +0200950 /* possible keywords: req.hdr_cnt / hdr_cnt, res.hdr_cnt / shdr_cnt */
951 struct channel *chn = ((kw[0] == 'h' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +0200952 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +0200953 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200954 struct http_hdr_ctx ctx;
955 struct ist name;
Willy Tarreau79e57332018-10-02 16:01:16 +0200956 int cnt;
Christopher Faulet311c7ea2018-10-24 21:41:55 +0200957
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200958 if (!htx)
959 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200960
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200961 if (args && args->type == ARGT_STR) {
962 name.ptr = args->data.str.area;
963 name.len = args->data.str.data;
964 } else {
965 name.ptr = NULL;
966 name.len = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +0200967 }
Willy Tarreau79e57332018-10-02 16:01:16 +0200968
Christopher Faulet6d1dd462019-07-15 14:36:03 +0200969 ctx.blk = NULL;
970 cnt = 0;
971 while (http_find_header(htx, name, &ctx, 0))
972 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +0200973
974 smp->data.type = SMP_T_SINT;
975 smp->data.u.sint = cnt;
976 smp->flags = SMP_F_VOL_HDR;
977 return 1;
978}
979
980/* Fetch an HTTP header's integer value. The integer value is returned. It
981 * takes a mandatory argument of type string and an optional one of type int
982 * to designate a specific occurrence. It returns an unsigned integer, which
983 * may or may not be appropriate for everything.
984 */
985static int smp_fetch_hdr_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
986{
987 int ret = smp_fetch_hdr(args, smp, kw, private);
988
989 if (ret > 0) {
990 smp->data.type = SMP_T_SINT;
991 smp->data.u.sint = strl2ic(smp->data.u.str.area,
992 smp->data.u.str.data);
993 }
994
995 return ret;
996}
997
998/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
999 * and an optional one of type int to designate a specific occurrence.
1000 * It returns an IPv4 or IPv6 address.
1001 */
1002static int smp_fetch_hdr_ip(const struct arg *args, struct sample *smp, const char *kw, void *private)
1003{
1004 int ret;
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001005 struct buffer *temp = get_trash_chunk();
Willy Tarreau79e57332018-10-02 16:01:16 +02001006
1007 while ((ret = smp_fetch_hdr(args, smp, kw, private)) > 0) {
Tim Duesterhus5cd00872020-06-26 15:44:48 +02001008 if (smp->data.u.str.data < temp->size - 1) {
1009 memcpy(temp->area, smp->data.u.str.area,
1010 smp->data.u.str.data);
1011 temp->area[smp->data.u.str.data] = '\0';
1012 if (url2ipv4((char *) temp->area, &smp->data.u.ipv4)) {
1013 smp->data.type = SMP_T_IPV4;
1014 break;
1015 } else if (inet_pton(AF_INET6, temp->area, &smp->data.u.ipv6)) {
1016 smp->data.type = SMP_T_IPV6;
1017 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001018 }
1019 }
1020
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001021 /* if the header doesn't match an IP address, fetch next one */
1022 if (!(smp->flags & SMP_F_NOT_LAST))
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001023 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001024 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001025 return ret;
1026}
Willy Tarreau79e57332018-10-02 16:01:16 +02001027
Christopher Faulete720c322020-09-02 17:25:18 +02001028/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at the
1029 * first '/' after the possible hostname. It ends before the possible '?' except
1030 * for 'pathq' keyword.
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001031 */
1032static int smp_fetch_path(const struct arg *args, struct sample *smp, const char *kw, void *private)
1033{
1034 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001035 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001036 struct htx_sl *sl;
1037 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001038
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001039 if (!htx)
1040 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001041
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001042 sl = http_get_stline(htx);
Christopher Faulete720c322020-09-02 17:25:18 +02001043 path = http_get_path(htx_sl_req_uri(sl));
1044
1045 if (kw[0] == 'p' && kw[4] == 'q') // pathq
1046 path = http_get_path(htx_sl_req_uri(sl));
1047 else
1048 path = iststop(http_get_path(htx_sl_req_uri(sl)), '?');
1049
Tim Duesterhused526372020-03-05 17:56:33 +01001050 if (!isttest(path))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001051 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001052
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001053 /* OK, we got the '/' ! */
1054 smp->data.type = SMP_T_STR;
1055 smp->data.u.str.area = path.ptr;
Jerome Magnin4fb196c2020-02-21 10:49:12 +01001056 smp->data.u.str.data = path.len;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001057 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau79e57332018-10-02 16:01:16 +02001058 return 1;
1059}
1060
1061/* This produces a concatenation of the first occurrence of the Host header
1062 * followed by the path component if it begins with a slash ('/'). This means
1063 * that '*' will not be added, resulting in exactly the first Host entry.
1064 * If no Host header is found, then the path is returned as-is. The returned
1065 * value is stored in the trash so it does not need to be marked constant.
1066 * The returned sample is of type string.
1067 */
1068static int smp_fetch_base(const struct arg *args, struct sample *smp, const char *kw, void *private)
1069{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001070 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001071 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001072 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001073 struct buffer *temp;
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001074 struct http_hdr_ctx ctx;
1075 struct ist path;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001076
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001077 if (!htx)
1078 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001079
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001080 ctx.blk = NULL;
1081 if (!http_find_header(htx, ist("Host"), &ctx, 0) || !ctx.value.len)
1082 return smp_fetch_path(args, smp, kw, private);
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001083
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001084 /* OK we have the header value in ctx.value */
1085 temp = get_trash_chunk();
1086 chunk_memcat(temp, ctx.value.ptr, ctx.value.len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001087
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001088 /* now retrieve the path */
1089 sl = http_get_stline(htx);
1090 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001091 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001092 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001093
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001094 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1095 ;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001096
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001097 if (len && *(path.ptr) == '/')
1098 chunk_memcat(temp, path.ptr, len);
Willy Tarreau79e57332018-10-02 16:01:16 +02001099 }
1100
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001101 smp->data.type = SMP_T_STR;
1102 smp->data.u.str = *temp;
Willy Tarreau79e57332018-10-02 16:01:16 +02001103 smp->flags = SMP_F_VOL_1ST;
1104 return 1;
1105}
1106
1107/* This produces a 32-bit hash of the concatenation of the first occurrence of
1108 * the Host header followed by the path component if it begins with a slash ('/').
1109 * This means that '*' will not be added, resulting in exactly the first Host
1110 * entry. If no Host header is found, then the path is used. The resulting value
1111 * is hashed using the path hash followed by a full avalanche hash and provides a
1112 * 32-bit integer value. This fetch is useful for tracking per-path activity on
1113 * high-traffic sites without having to store whole paths.
1114 */
1115static int smp_fetch_base32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1116{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001117 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001118 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001119 struct htx_sl *sl;
1120 struct http_hdr_ctx ctx;
1121 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001122 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001123
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001124 if (!htx)
1125 return 0;
Dragan Dosen8861e1c2019-02-12 19:50:31 +01001126
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001127 ctx.blk = NULL;
1128 if (http_find_header(htx, ist("Host"), &ctx, 0)) {
1129 /* OK we have the header value in ctx.value */
1130 while (ctx.value.len--)
1131 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001132 }
1133
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001134 /* now retrieve the path */
1135 sl = http_get_stline(htx);
1136 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01001137 if (isttest(path)) {
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001138 size_t len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001139
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001140 for (len = 0; len < path.len && *(path.ptr + len) != '?'; len++)
1141 ;
Willy Tarreau79e57332018-10-02 16:01:16 +02001142
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001143 if (len && *(path.ptr) == '/') {
1144 while (len--)
1145 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001146 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001147 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001148
Willy Tarreau79e57332018-10-02 16:01:16 +02001149 hash = full_hash(hash);
1150
1151 smp->data.type = SMP_T_SINT;
1152 smp->data.u.sint = hash;
1153 smp->flags = SMP_F_VOL_1ST;
1154 return 1;
1155}
1156
1157/* This concatenates the source address with the 32-bit hash of the Host and
1158 * path as returned by smp_fetch_base32(). The idea is to have per-source and
1159 * per-path counters. The result is a binary block from 8 to 20 bytes depending
1160 * on the source address length. The path hash is stored before the address so
1161 * that in environments where IPv6 is insignificant, truncating the output to
1162 * 8 bytes would still work.
1163 */
1164static int smp_fetch_base32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1165{
1166 struct buffer *temp;
1167 struct connection *cli_conn = objt_conn(smp->sess->origin);
1168
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001169 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001170 return 0;
1171
1172 if (!smp_fetch_base32(args, smp, kw, private))
1173 return 0;
1174
1175 temp = get_trash_chunk();
1176 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1177 temp->data += sizeof(unsigned int);
1178
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001179 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001180 case AF_INET:
1181 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001182 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001183 4);
1184 temp->data += 4;
1185 break;
1186 case AF_INET6:
1187 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001188 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001189 16);
1190 temp->data += 16;
1191 break;
1192 default:
1193 return 0;
1194 }
1195
1196 smp->data.u.str = *temp;
1197 smp->data.type = SMP_T_BIN;
1198 return 1;
1199}
1200
1201/* Extracts the query string, which comes after the question mark '?'. If no
1202 * question mark is found, nothing is returned. Otherwise it returns a sample
1203 * of type string carrying the whole query string.
1204 */
1205static int smp_fetch_query(const struct arg *args, struct sample *smp, const char *kw, void *private)
1206{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001207 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001208 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001209 struct htx_sl *sl;
Willy Tarreau79e57332018-10-02 16:01:16 +02001210 char *ptr, *end;
1211
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001212 if (!htx)
1213 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001214
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001215 sl = http_get_stline(htx);
1216 ptr = HTX_SL_REQ_UPTR(sl);
1217 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001218
1219 /* look up the '?' */
1220 do {
1221 if (ptr == end)
1222 return 0;
1223 } while (*ptr++ != '?');
1224
1225 smp->data.type = SMP_T_STR;
1226 smp->data.u.str.area = ptr;
1227 smp->data.u.str.data = end - ptr;
1228 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1229 return 1;
1230}
1231
1232static int smp_fetch_proto_http(const struct arg *args, struct sample *smp, const char *kw, void *private)
1233{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001234 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001235 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 0);
Willy Tarreau79e57332018-10-02 16:01:16 +02001236
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001237 if (!htx)
1238 return 0;
1239 smp->data.type = SMP_T_BOOL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001240 smp->data.u.sint = 1;
1241 return 1;
1242}
1243
1244/* return a valid test if the current request is the first one on the connection */
1245static int smp_fetch_http_first_req(const struct arg *args, struct sample *smp, const char *kw, void *private)
1246{
Willy Tarreau79512b62020-04-29 11:52:13 +02001247 if (!smp->strm)
1248 return 0;
1249
Willy Tarreau79e57332018-10-02 16:01:16 +02001250 smp->data.type = SMP_T_BOOL;
1251 smp->data.u.sint = !(smp->strm->txn->flags & TX_NOT_FIRST);
1252 return 1;
1253}
1254
Christopher Fauleta4063562019-08-02 11:51:37 +02001255/* Fetch the authentication method if there is an Authorization header. It
1256 * relies on get_http_auth()
1257 */
1258static int smp_fetch_http_auth_type(const struct arg *args, struct sample *smp, const char *kw, void *private)
1259{
1260 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001261 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001262 struct http_txn *txn;
1263
1264 if (!htx)
1265 return 0;
1266
1267 txn = smp->strm->txn;
1268 if (!get_http_auth(smp, htx))
1269 return 0;
1270
1271 switch (txn->auth.method) {
1272 case HTTP_AUTH_BASIC:
1273 smp->data.u.str.area = "Basic";
1274 smp->data.u.str.data = 5;
1275 break;
1276 case HTTP_AUTH_DIGEST:
1277 /* Unexpected because not supported */
1278 smp->data.u.str.area = "Digest";
1279 smp->data.u.str.data = 6;
1280 break;
1281 default:
1282 return 0;
1283 }
1284
1285 smp->data.type = SMP_T_STR;
1286 smp->flags = SMP_F_CONST;
1287 return 1;
1288}
1289
1290/* Fetch the user supplied if there is an Authorization header. It relies on
1291 * get_http_auth()
1292 */
1293static int smp_fetch_http_auth_user(const struct arg *args, struct sample *smp, const char *kw, void *private)
1294{
1295 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001296 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001297 struct http_txn *txn;
1298
1299 if (!htx)
1300 return 0;
1301
1302 txn = smp->strm->txn;
1303 if (!get_http_auth(smp, htx))
1304 return 0;
1305
1306 smp->data.type = SMP_T_STR;
1307 smp->data.u.str.area = txn->auth.user;
1308 smp->data.u.str.data = strlen(txn->auth.user);
1309 smp->flags = SMP_F_CONST;
1310 return 1;
1311}
1312
1313/* Fetch the password supplied if there is an Authorization header. It relies on
1314 * get_http_auth()
1315 */
1316static int smp_fetch_http_auth_pass(const struct arg *args, struct sample *smp, const char *kw, void *private)
1317{
1318 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001319 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Fauleta4063562019-08-02 11:51:37 +02001320 struct http_txn *txn;
1321
1322 if (!htx)
1323 return 0;
1324
1325 txn = smp->strm->txn;
1326 if (!get_http_auth(smp, htx))
1327 return 0;
1328
1329 smp->data.type = SMP_T_STR;
1330 smp->data.u.str.area = txn->auth.pass;
1331 smp->data.u.str.data = strlen(txn->auth.pass);
1332 smp->flags = SMP_F_CONST;
1333 return 1;
1334}
1335
Willy Tarreau79e57332018-10-02 16:01:16 +02001336/* Accepts exactly 1 argument of type userlist */
1337static int smp_fetch_http_auth(const struct arg *args, struct sample *smp, const char *kw, void *private)
1338{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001339 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001340 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Willy Tarreau79e57332018-10-02 16:01:16 +02001341
1342 if (!args || args->type != ARGT_USR)
1343 return 0;
1344
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001345 if (!htx)
1346 return 0;
1347 if (!get_http_auth(smp, htx))
1348 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001349
1350 smp->data.type = SMP_T_BOOL;
1351 smp->data.u.sint = check_user(args->data.usr, smp->strm->txn->auth.user,
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001352 smp->strm->txn->auth.pass);
Willy Tarreau79e57332018-10-02 16:01:16 +02001353 return 1;
1354}
1355
1356/* Accepts exactly 1 argument of type userlist */
1357static int smp_fetch_http_auth_grp(const struct arg *args, struct sample *smp, const char *kw, void *private)
1358{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001359 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001360 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet89dc4992019-04-17 12:02:59 +02001361
Willy Tarreau79e57332018-10-02 16:01:16 +02001362 if (!args || args->type != ARGT_USR)
1363 return 0;
1364
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001365 if (!htx)
1366 return 0;
1367 if (!get_http_auth(smp, htx))
1368 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001369
Willy Tarreau79e57332018-10-02 16:01:16 +02001370 /* if the user does not belong to the userlist or has a wrong password,
1371 * report that it unconditionally does not match. Otherwise we return
1372 * a string containing the username.
1373 */
1374 if (!check_user(args->data.usr, smp->strm->txn->auth.user,
1375 smp->strm->txn->auth.pass))
1376 return 0;
1377
1378 /* pat_match_auth() will need the user list */
1379 smp->ctx.a[0] = args->data.usr;
1380
1381 smp->data.type = SMP_T_STR;
1382 smp->flags = SMP_F_CONST;
1383 smp->data.u.str.area = smp->strm->txn->auth.user;
1384 smp->data.u.str.data = strlen(smp->strm->txn->auth.user);
1385
1386 return 1;
1387}
1388
1389/* Fetch a captured HTTP request header. The index is the position of
1390 * the "capture" option in the configuration file
1391 */
1392static int smp_fetch_capture_req_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1393{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001394 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001395 int idx;
1396
1397 if (!args || args->type != ARGT_SINT)
1398 return 0;
1399
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001400 if (!smp->strm)
1401 return 0;
1402
1403 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001404 idx = args->data.sint;
1405
1406 if (idx > (fe->nb_req_cap - 1) || smp->strm->req_cap == NULL || smp->strm->req_cap[idx] == NULL)
1407 return 0;
1408
1409 smp->data.type = SMP_T_STR;
1410 smp->flags |= SMP_F_CONST;
1411 smp->data.u.str.area = smp->strm->req_cap[idx];
1412 smp->data.u.str.data = strlen(smp->strm->req_cap[idx]);
1413
1414 return 1;
1415}
1416
1417/* Fetch a captured HTTP response header. The index is the position of
1418 * the "capture" option in the configuration file
1419 */
1420static int smp_fetch_capture_res_hdr(const struct arg *args, struct sample *smp, const char *kw, void *private)
1421{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001422 struct proxy *fe;
Willy Tarreau79e57332018-10-02 16:01:16 +02001423 int idx;
1424
1425 if (!args || args->type != ARGT_SINT)
1426 return 0;
1427
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001428 if (!smp->strm)
1429 return 0;
1430
1431 fe = strm_fe(smp->strm);
Willy Tarreau79e57332018-10-02 16:01:16 +02001432 idx = args->data.sint;
1433
1434 if (idx > (fe->nb_rsp_cap - 1) || smp->strm->res_cap == NULL || smp->strm->res_cap[idx] == NULL)
1435 return 0;
1436
1437 smp->data.type = SMP_T_STR;
1438 smp->flags |= SMP_F_CONST;
1439 smp->data.u.str.area = smp->strm->res_cap[idx];
1440 smp->data.u.str.data = strlen(smp->strm->res_cap[idx]);
1441
1442 return 1;
1443}
1444
1445/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
1446static int smp_fetch_capture_req_method(const struct arg *args, struct sample *smp, const char *kw, void *private)
1447{
1448 struct buffer *temp;
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001449 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001450 char *ptr;
1451
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001452 if (!smp->strm)
1453 return 0;
1454
1455 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001456 if (!txn || !txn->uri)
1457 return 0;
1458
1459 ptr = txn->uri;
1460
1461 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1462 ptr++;
1463
1464 temp = get_trash_chunk();
1465 temp->area = txn->uri;
1466 temp->data = ptr - txn->uri;
1467 smp->data.u.str = *temp;
1468 smp->data.type = SMP_T_STR;
1469 smp->flags = SMP_F_CONST;
1470
1471 return 1;
1472
1473}
1474
1475/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
1476static int smp_fetch_capture_req_uri(const struct arg *args, struct sample *smp, const char *kw, void *private)
1477{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001478 struct http_txn *txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001479 struct ist path;
1480 const char *ptr;
1481
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001482 if (!smp->strm)
1483 return 0;
1484
1485 txn = smp->strm->txn;
Willy Tarreau79e57332018-10-02 16:01:16 +02001486 if (!txn || !txn->uri)
1487 return 0;
1488
1489 ptr = txn->uri;
1490
1491 while (*ptr != ' ' && *ptr != '\0') /* find first space */
1492 ptr++;
1493
1494 if (!*ptr)
1495 return 0;
1496
Christopher Faulet78337bb2018-11-15 14:35:18 +01001497 /* skip the first space and find space after URI */
1498 path = ist2(++ptr, 0);
1499 while (*ptr != ' ' && *ptr != '\0')
1500 ptr++;
1501 path.len = ptr - path.ptr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001502
Christopher Faulet78337bb2018-11-15 14:35:18 +01001503 path = http_get_path(path);
Tim Duesterhused526372020-03-05 17:56:33 +01001504 if (!isttest(path))
Willy Tarreau79e57332018-10-02 16:01:16 +02001505 return 0;
1506
1507 smp->data.u.str.area = path.ptr;
1508 smp->data.u.str.data = path.len;
1509 smp->data.type = SMP_T_STR;
1510 smp->flags = SMP_F_CONST;
1511
1512 return 1;
1513}
1514
1515/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
1516 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1517 */
1518static int smp_fetch_capture_req_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1519{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001520 struct http_txn *txn;
1521
1522 if (!smp->strm)
1523 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001524
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001525 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001526 if (!txn || txn->req.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001527 return 0;
1528
1529 if (txn->req.flags & HTTP_MSGF_VER_11)
1530 smp->data.u.str.area = "HTTP/1.1";
1531 else
1532 smp->data.u.str.area = "HTTP/1.0";
1533
1534 smp->data.u.str.data = 8;
1535 smp->data.type = SMP_T_STR;
1536 smp->flags = SMP_F_CONST;
1537 return 1;
1538
1539}
1540
1541/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
1542 * as a string (either "HTTP/1.0" or "HTTP/1.1").
1543 */
1544static int smp_fetch_capture_res_ver(const struct arg *args, struct sample *smp, const char *kw, void *private)
1545{
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001546 struct http_txn *txn;
1547
1548 if (!smp->strm)
1549 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001550
Willy Tarreau0898c2d2020-04-29 11:44:54 +02001551 txn = smp->strm->txn;
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001552 if (!txn || txn->rsp.msg_state >= HTTP_MSG_BODY)
Willy Tarreau79e57332018-10-02 16:01:16 +02001553 return 0;
1554
1555 if (txn->rsp.flags & HTTP_MSGF_VER_11)
1556 smp->data.u.str.area = "HTTP/1.1";
1557 else
1558 smp->data.u.str.area = "HTTP/1.0";
1559
1560 smp->data.u.str.data = 8;
1561 smp->data.type = SMP_T_STR;
1562 smp->flags = SMP_F_CONST;
1563 return 1;
1564
1565}
1566
1567/* Iterate over all cookies present in a message. The context is stored in
1568 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
1569 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
1570 * the direction, multiple cookies may be parsed on the same line or not.
1571 * The cookie name is in args and the name length in args->data.str.len.
1572 * Accepts exactly 1 argument of type string. If the input options indicate
1573 * that no iterating is desired, then only last value is fetched if any.
1574 * The returned sample is of type CSTR. Can be used to parse cookies in other
1575 * files.
1576 */
1577static int smp_fetch_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1578{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001579 /* possible keywords: req.cookie / cookie / cook, res.cookie / scook / set-cookie */
1580 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001581 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001582 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001583 struct http_hdr_ctx *ctx = smp->ctx.a[2];
1584 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001585 int occ = 0;
1586 int found = 0;
1587
1588 if (!args || args->type != ARGT_STR)
1589 return 0;
1590
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001591 if (!ctx) {
1592 /* first call */
1593 ctx = &static_http_hdr_ctx;
1594 ctx->blk = NULL;
1595 smp->ctx.a[2] = ctx;
1596 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001597
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001598 if (!htx)
1599 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001600
Christopher Faulet16032ab2020-04-30 11:30:00 +02001601 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001602
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001603 if (!occ && !(smp->opt & SMP_OPT_ITERATE))
1604 /* no explicit occurrence and single fetch => last cookie by default */
1605 occ = -1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001606
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001607 /* OK so basically here, either we want only one value and it's the
1608 * last one, or we want to iterate over all of them and we fetch the
1609 * next one.
1610 */
Willy Tarreau79e57332018-10-02 16:01:16 +02001611
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001612 if (!(smp->flags & SMP_F_NOT_LAST)) {
1613 /* search for the header from the beginning, we must first initialize
1614 * the search parameters.
Willy Tarreau79e57332018-10-02 16:01:16 +02001615 */
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001616 smp->ctx.a[0] = NULL;
1617 ctx->blk = NULL;
Willy Tarreau79e57332018-10-02 16:01:16 +02001618 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001619
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001620 smp->flags |= SMP_F_VOL_HDR;
1621 while (1) {
1622 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
1623 if (!smp->ctx.a[0]) {
1624 if (!http_find_header(htx, hdr, ctx, 0))
1625 goto out;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001626
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001627 if (ctx->value.len < args->data.str.data + 1)
1628 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001629
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001630 smp->ctx.a[0] = ctx->value.ptr;
1631 smp->ctx.a[1] = smp->ctx.a[0] + ctx->value.len;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001632 }
1633
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001634 smp->data.type = SMP_T_STR;
1635 smp->flags |= SMP_F_CONST;
1636 smp->ctx.a[0] = http_extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
1637 args->data.str.area, args->data.str.data,
1638 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1639 &smp->data.u.str.area,
1640 &smp->data.u.str.data);
1641 if (smp->ctx.a[0]) {
1642 found = 1;
1643 if (occ >= 0) {
1644 /* one value was returned into smp->data.u.str.{str,len} */
1645 smp->flags |= SMP_F_NOT_LAST;
1646 return 1;
Willy Tarreau79e57332018-10-02 16:01:16 +02001647 }
1648 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001649 /* if we're looking for last occurrence, let's loop */
Willy Tarreau79e57332018-10-02 16:01:16 +02001650 }
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001651
Willy Tarreau79e57332018-10-02 16:01:16 +02001652 /* all cookie headers and values were scanned. If we're looking for the
1653 * last occurrence, we may return it now.
1654 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001655 out:
Willy Tarreau79e57332018-10-02 16:01:16 +02001656 smp->flags &= ~SMP_F_NOT_LAST;
1657 return found;
1658}
1659
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02001660/* Same than smp_fetch_cookie() but only relies on the sample direction to
1661 * choose the right channel. So instead of duplicating the code, we just change
1662 * the keyword and then fallback on smp_fetch_cookie().
1663 */
1664static int smp_fetch_chn_cookie(const struct arg *args, struct sample *smp, const char *kw, void *private)
1665{
1666 kw = ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ ? "req.cook" : "res.cook");
1667 return smp_fetch_cookie(args, smp, kw, private);
1668}
1669
Willy Tarreau79e57332018-10-02 16:01:16 +02001670/* Iterate over all cookies present in a request to count how many occurrences
1671 * match the name in args and args->data.str.len. If <multi> is non-null, then
1672 * multiple cookies may be parsed on the same line. The returned sample is of
1673 * type UINT. Accepts exactly 1 argument of type string.
1674 */
1675static int smp_fetch_cookie_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
1676{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001677 /* possible keywords: req.cook_cnt / cook_cnt, res.cook_cnt / scook_cnt */
1678 struct channel *chn = ((kw[0] == 'c' || kw[2] == 'q') ? SMP_REQ_CHN(smp) : SMP_RES_CHN(smp));
Christopher Fauletf98e6262020-05-06 09:42:04 +02001679 struct check *check = ((kw[0] == 's' || kw[2] == 's') ? objt_check(smp->sess->origin) : NULL);
Christopher Faulet16032ab2020-04-30 11:30:00 +02001680 struct htx *htx = smp_prefetch_htx(smp, chn, check, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001681 struct http_hdr_ctx ctx;
1682 struct ist hdr;
Willy Tarreau79e57332018-10-02 16:01:16 +02001683 char *val_beg, *val_end;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001684 int cnt;
Willy Tarreau79e57332018-10-02 16:01:16 +02001685
1686 if (!args || args->type != ARGT_STR)
1687 return 0;
1688
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001689 if (!htx)
1690 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001691
Christopher Faulet16032ab2020-04-30 11:30:00 +02001692 hdr = (!(check || (chn && chn->flags & CF_ISRESP)) ? ist("Cookie") : ist("Set-Cookie"));
Willy Tarreau79e57332018-10-02 16:01:16 +02001693
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001694 val_end = val_beg = NULL;
1695 ctx.blk = NULL;
1696 cnt = 0;
1697 while (1) {
1698 /* Note: val_beg == NULL every time we need to fetch a new header */
1699 if (!val_beg) {
1700 if (!http_find_header(htx, hdr, &ctx, 0))
1701 break;
Willy Tarreau79e57332018-10-02 16:01:16 +02001702
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001703 if (ctx.value.len < args->data.str.data + 1)
1704 continue;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001705
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001706 val_beg = ctx.value.ptr;
1707 val_end = val_beg + ctx.value.len;
Willy Tarreau79e57332018-10-02 16:01:16 +02001708 }
1709
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001710 smp->data.type = SMP_T_STR;
1711 smp->flags |= SMP_F_CONST;
1712 while ((val_beg = http_extract_cookie_value(val_beg, val_end,
1713 args->data.str.area, args->data.str.data,
1714 (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
1715 &smp->data.u.str.area,
1716 &smp->data.u.str.data))) {
1717 cnt++;
Willy Tarreau79e57332018-10-02 16:01:16 +02001718 }
1719 }
1720
1721 smp->data.type = SMP_T_SINT;
1722 smp->data.u.sint = cnt;
1723 smp->flags |= SMP_F_VOL_HDR;
1724 return 1;
1725}
1726
1727/* Fetch an cookie's integer value. The integer value is returned. It
1728 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
1729 */
1730static int smp_fetch_cookie_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1731{
1732 int ret = smp_fetch_cookie(args, smp, kw, private);
1733
1734 if (ret > 0) {
1735 smp->data.type = SMP_T_SINT;
1736 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1737 smp->data.u.str.data);
1738 }
1739
1740 return ret;
1741}
1742
1743/************************************************************************/
1744/* The code below is dedicated to sample fetches */
1745/************************************************************************/
1746
1747/* This scans a URL-encoded query string. It takes an optionally wrapping
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001748 * string whose first contiguous chunk has its beginning in ctx->a[0] and end
Willy Tarreau79e57332018-10-02 16:01:16 +02001749 * in ctx->a[1], and the optional second part in (ctx->a[2]..ctx->a[3]). The
1750 * pointers are updated for next iteration before leaving.
1751 */
1752static int smp_fetch_param(char delim, const char *name, int name_len, const struct arg *args, struct sample *smp, const char *kw, void *private)
1753{
1754 const char *vstart, *vend;
1755 struct buffer *temp;
1756 const char **chunks = (const char **)smp->ctx.a;
1757
1758 if (!http_find_next_url_param(chunks, name, name_len,
1759 &vstart, &vend, delim))
1760 return 0;
1761
1762 /* Create sample. If the value is contiguous, return the pointer as CONST,
1763 * if the value is wrapped, copy-it in a buffer.
1764 */
1765 smp->data.type = SMP_T_STR;
1766 if (chunks[2] &&
1767 vstart >= chunks[0] && vstart <= chunks[1] &&
1768 vend >= chunks[2] && vend <= chunks[3]) {
1769 /* Wrapped case. */
1770 temp = get_trash_chunk();
1771 memcpy(temp->area, vstart, chunks[1] - vstart);
1772 memcpy(temp->area + ( chunks[1] - vstart ), chunks[2],
1773 vend - chunks[2]);
1774 smp->data.u.str.area = temp->area;
1775 smp->data.u.str.data = ( chunks[1] - vstart ) + ( vend - chunks[2] );
1776 } else {
1777 /* Contiguous case. */
1778 smp->data.u.str.area = (char *)vstart;
1779 smp->data.u.str.data = vend - vstart;
1780 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
1781 }
1782
1783 /* Update context, check wrapping. */
1784 chunks[0] = vend;
1785 if (chunks[2] && vend >= chunks[2] && vend <= chunks[3]) {
1786 chunks[1] = chunks[3];
1787 chunks[2] = NULL;
1788 }
1789
1790 if (chunks[0] < chunks[1])
1791 smp->flags |= SMP_F_NOT_LAST;
1792
1793 return 1;
1794}
1795
1796/* This function iterates over each parameter of the query string. It uses
1797 * ctx->a[0] and ctx->a[1] to store the beginning and end of the current
1798 * parameter. Since it uses smp_fetch_param(), ctx->a[2..3] are both NULL.
1799 * An optional parameter name is passed in args[0], otherwise any parameter is
1800 * considered. It supports an optional delimiter argument for the beginning of
1801 * the string in args[1], which defaults to "?".
1802 */
1803static int smp_fetch_url_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1804{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001805 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001806 char delim = '?';
1807 const char *name;
1808 int name_len;
1809
1810 if (!args ||
1811 (args[0].type && args[0].type != ARGT_STR) ||
1812 (args[1].type && args[1].type != ARGT_STR))
1813 return 0;
1814
1815 name = "";
1816 name_len = 0;
1817 if (args->type == ARGT_STR) {
1818 name = args->data.str.area;
1819 name_len = args->data.str.data;
1820 }
1821
1822 if (args[1].type)
1823 delim = *args[1].data.str.area;
1824
1825 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001826 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001827 struct htx_sl *sl;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001828
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001829 if (!htx)
1830 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001831
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001832 sl = http_get_stline(htx);
1833 smp->ctx.a[0] = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), delim);
1834 if (!smp->ctx.a[0])
1835 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001836
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001837 smp->ctx.a[1] = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Willy Tarreau79e57332018-10-02 16:01:16 +02001838
1839 /* Assume that the context is filled with NULL pointer
1840 * before the first call.
1841 * smp->ctx.a[2] = NULL;
1842 * smp->ctx.a[3] = NULL;
1843 */
1844 }
1845
1846 return smp_fetch_param(delim, name, name_len, args, smp, kw, private);
1847}
1848
1849/* This function iterates over each parameter of the body. This requires
1850 * that the body has been waited for using http-buffer-request. It uses
1851 * ctx->a[0] and ctx->a[1] to store the beginning and end of the first
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001852 * contiguous part of the body, and optionally ctx->a[2..3] to reference the
Willy Tarreau79e57332018-10-02 16:01:16 +02001853 * optional second part if the body wraps at the end of the buffer. An optional
1854 * parameter name is passed in args[0], otherwise any parameter is considered.
1855 */
1856static int smp_fetch_body_param(const struct arg *args, struct sample *smp, const char *kw, void *private)
1857{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001858 struct channel *chn = SMP_REQ_CHN(smp);
Willy Tarreau79e57332018-10-02 16:01:16 +02001859 const char *name;
1860 int name_len;
1861
1862 if (!args || (args[0].type && args[0].type != ARGT_STR))
1863 return 0;
1864
1865 name = "";
1866 name_len = 0;
1867 if (args[0].type == ARGT_STR) {
1868 name = args[0].data.str.area;
1869 name_len = args[0].data.str.data;
1870 }
1871
1872 if (!smp->ctx.a[0]) { // first call, find the query string
Christopher Faulete596d182020-05-05 17:46:34 +02001873 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001874 struct buffer *temp;
1875 int32_t pos;
Willy Tarreau79e57332018-10-02 16:01:16 +02001876
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001877 if (!htx)
1878 return 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001879
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001880 temp = get_trash_chunk();
1881 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
1882 struct htx_blk *blk = htx_get_blk(htx, pos);
1883 enum htx_blk_type type = htx_get_blk_type(blk);
Willy Tarreau79e57332018-10-02 16:01:16 +02001884
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001885 if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT)
1886 break;
1887 if (type == HTX_BLK_DATA) {
Christopher Faulet53a899b2019-10-08 16:38:42 +02001888 if (!h1_format_htx_data(htx_get_blk_value(htx, blk), temp, 0))
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001889 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001890 }
Willy Tarreau79e57332018-10-02 16:01:16 +02001891 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001892
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001893 smp->ctx.a[0] = temp->area;
1894 smp->ctx.a[1] = temp->area + temp->data;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001895
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001896 /* Assume that the context is filled with NULL pointer
1897 * before the first call.
1898 * smp->ctx.a[2] = NULL;
1899 * smp->ctx.a[3] = NULL;
1900 */
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001901
Willy Tarreau79e57332018-10-02 16:01:16 +02001902 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001903
Willy Tarreau79e57332018-10-02 16:01:16 +02001904 return smp_fetch_param('&', name, name_len, args, smp, kw, private);
1905}
1906
1907/* Return the signed integer value for the specified url parameter (see url_param
1908 * above).
1909 */
1910static int smp_fetch_url_param_val(const struct arg *args, struct sample *smp, const char *kw, void *private)
1911{
1912 int ret = smp_fetch_url_param(args, smp, kw, private);
1913
1914 if (ret > 0) {
1915 smp->data.type = SMP_T_SINT;
1916 smp->data.u.sint = strl2ic(smp->data.u.str.area,
1917 smp->data.u.str.data);
1918 }
1919
1920 return ret;
1921}
1922
1923/* This produces a 32-bit hash of the concatenation of the first occurrence of
1924 * the Host header followed by the path component if it begins with a slash ('/').
1925 * This means that '*' will not be added, resulting in exactly the first Host
1926 * entry. If no Host header is found, then the path is used. The resulting value
1927 * is hashed using the url hash followed by a full avalanche hash and provides a
1928 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
1929 * high-traffic sites without having to store whole paths.
1930 * this differs from the base32 functions in that it includes the url parameters
1931 * as well as the path
1932 */
1933static int smp_fetch_url32(const struct arg *args, struct sample *smp, const char *kw, void *private)
1934{
Christopher Faulet89dc4992019-04-17 12:02:59 +02001935 struct channel *chn = SMP_REQ_CHN(smp);
Christopher Faulet778f5ed2020-04-29 15:51:55 +02001936 struct htx *htx = smp_prefetch_htx(smp, chn, NULL, 1);
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001937 struct http_hdr_ctx ctx;
1938 struct htx_sl *sl;
1939 struct ist path;
Willy Tarreau79e57332018-10-02 16:01:16 +02001940 unsigned int hash = 0;
Willy Tarreau79e57332018-10-02 16:01:16 +02001941
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001942 if (!htx)
1943 return 0;
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001944
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001945 ctx.blk = NULL;
1946 if (http_find_header(htx, ist("Host"), &ctx, 1)) {
1947 /* OK we have the header value in ctx.value */
1948 while (ctx.value.len--)
1949 hash = *(ctx.value.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001950 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001951
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001952 /* now retrieve the path */
1953 sl = http_get_stline(htx);
1954 path = http_get_path(htx_sl_req_uri(sl));
Christopher Faulet6d1dd462019-07-15 14:36:03 +02001955 if (path.len && *(path.ptr) == '/') {
1956 while (path.len--)
1957 hash = *(path.ptr++) + (hash << 6) + (hash << 16) - hash;
Willy Tarreau79e57332018-10-02 16:01:16 +02001958 }
Christopher Faulet311c7ea2018-10-24 21:41:55 +02001959
Willy Tarreau79e57332018-10-02 16:01:16 +02001960 hash = full_hash(hash);
1961
1962 smp->data.type = SMP_T_SINT;
1963 smp->data.u.sint = hash;
1964 smp->flags = SMP_F_VOL_1ST;
1965 return 1;
1966}
1967
1968/* This concatenates the source address with the 32-bit hash of the Host and
1969 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
1970 * per-url counters. The result is a binary block from 8 to 20 bytes depending
1971 * on the source address length. The URL hash is stored before the address so
1972 * that in environments where IPv6 is insignificant, truncating the output to
1973 * 8 bytes would still work.
1974 */
1975static int smp_fetch_url32_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
1976{
1977 struct buffer *temp;
1978 struct connection *cli_conn = objt_conn(smp->sess->origin);
1979
Willy Tarreaucd7ca792019-07-17 16:57:03 +02001980 if (!cli_conn || !conn_get_src(cli_conn))
Willy Tarreau79e57332018-10-02 16:01:16 +02001981 return 0;
1982
1983 if (!smp_fetch_url32(args, smp, kw, private))
1984 return 0;
1985
1986 temp = get_trash_chunk();
1987 *(unsigned int *) temp->area = htonl(smp->data.u.sint);
1988 temp->data += sizeof(unsigned int);
1989
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001990 switch (cli_conn->src->ss_family) {
Willy Tarreau79e57332018-10-02 16:01:16 +02001991 case AF_INET:
1992 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001993 &((struct sockaddr_in *)cli_conn->src)->sin_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02001994 4);
1995 temp->data += 4;
1996 break;
1997 case AF_INET6:
1998 memcpy(temp->area + temp->data,
Willy Tarreau9a1efe12019-07-17 17:13:50 +02001999 &((struct sockaddr_in6 *)cli_conn->src)->sin6_addr,
Willy Tarreau79e57332018-10-02 16:01:16 +02002000 16);
2001 temp->data += 16;
2002 break;
2003 default:
2004 return 0;
2005 }
2006
2007 smp->data.u.str = *temp;
2008 smp->data.type = SMP_T_BIN;
2009 return 1;
2010}
2011
2012/************************************************************************/
2013/* Other utility functions */
2014/************************************************************************/
2015
2016/* This function is used to validate the arguments passed to any "hdr" fetch
2017 * keyword. These keywords support an optional positive or negative occurrence
2018 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
2019 * is assumed that the types are already the correct ones. Returns 0 on error,
2020 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
2021 * error message in case of error, that the caller is responsible for freeing.
2022 * The initial location must either be freeable or NULL.
2023 * Note: this function's pointer is checked from Lua.
2024 */
2025int val_hdr(struct arg *arg, char **err_msg)
2026{
2027 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
2028 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
2029 return 0;
2030 }
2031 return 1;
2032}
2033
2034/************************************************************************/
2035/* All supported sample fetch keywords must be declared here. */
2036/************************************************************************/
2037
2038/* Note: must not be declared <const> as its list will be overwritten */
2039static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
2040 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2041 { "base32", smp_fetch_base32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2042 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2043
2044 /* capture are allocated and are permanent in the stream */
2045 { "capture.req.hdr", smp_fetch_capture_req_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
2046
2047 /* retrieve these captures from the HTTP logs */
2048 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2049 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2050 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2051
2052 { "capture.res.hdr", smp_fetch_capture_res_hdr, ARG1(1,SINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
2053 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
2054
2055 /* cookie is valid in both directions (eg: for "stick ...") but cook*
2056 * are only here to match the ACL's name, are request-only and are used
2057 * for ACL compatibility only.
2058 */
2059 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002060 { "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 +02002061 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2062 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2063
2064 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
2065 * only here to match the ACL's name, are request-only and are used for
2066 * ACL compatibility only.
2067 */
Christopher Fauletc1f40dd2019-05-16 10:07:30 +02002068 { "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 +02002069 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2070 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2071 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2072
Christopher Fauleta4063562019-08-02 11:51:37 +02002073 { "http_auth_type", smp_fetch_http_auth_type, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2074 { "http_auth_user", smp_fetch_http_auth_user, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2075 { "http_auth_pass", smp_fetch_http_auth_pass, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002076 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
2077 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2078 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2079 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
2080 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Christopher Faulete720c322020-09-02 17:25:18 +02002081 { "pathq", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau79e57332018-10-02 16:01:16 +02002082 { "query", smp_fetch_query, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2083
2084 /* HTTP protocol on the request path */
2085 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2086 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
2087
2088 /* HTTP version on the request path */
2089 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2090 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2091
2092 { "req.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2093 { "req.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2094 { "req.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2095 { "req.body_param", smp_fetch_body_param, ARG1(0,STR), NULL, SMP_T_BIN, SMP_USE_HRQHV },
2096
2097 { "req.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2098 { "req.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2099
2100 /* HTTP version on the response path */
2101 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2102 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
2103
Christopher Faulete596d182020-05-05 17:46:34 +02002104 { "res.body", smp_fetch_body, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2105 { "res.body_len", smp_fetch_body_len, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2106 { "res.body_size", smp_fetch_body_size, 0, NULL, SMP_T_SINT, SMP_USE_HRSHV },
2107
2108 { "res.hdrs", smp_fetch_hdrs, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2109 { "res.hdrs_bin", smp_fetch_hdrs_bin, 0, NULL, SMP_T_BIN, SMP_USE_HRSHV },
2110
Willy Tarreau79e57332018-10-02 16:01:16 +02002111 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
2112 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2113 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2114 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2115
2116 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2117 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2118 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
2119 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRQHV },
2120 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
2121 { "req.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2122 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRQHV },
2123
2124 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
2125 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2126 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2127 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2128
2129 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2130 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2131 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2132 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2133 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2134 { "res.hdr_names", smp_fetch_hdr_names, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2135 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2136
2137 /* scook is valid only on the response and is used for ACL compatibility */
2138 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
2139 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2140 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2141 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
2142
2143 /* shdr is valid only on the response and is used for ACL compatibility */
2144 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
2145 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_SINT, SMP_USE_HRSHV },
2146 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
2147 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_SINT, SMP_USE_HRSHV },
2148
2149 { "status", smp_fetch_stcode, 0, NULL, SMP_T_SINT, SMP_USE_HRSHP },
2150 { "unique-id", smp_fetch_uniqueid, 0, NULL, SMP_T_STR, SMP_SRC_L4SRV },
2151 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
2152 { "url32", smp_fetch_url32, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2153 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
2154 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
2155 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_SINT, SMP_USE_HRQHV },
2156 { "url_param", smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2157 { "urlp" , smp_fetch_url_param, ARG2(0,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
2158 { "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 +02002159
Willy Tarreau79e57332018-10-02 16:01:16 +02002160 { /* END */ },
2161}};
2162
Willy Tarreau0108d902018-11-25 19:14:37 +01002163INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau79e57332018-10-02 16:01:16 +02002164
2165/*
2166 * Local variables:
2167 * c-indent-level: 8
2168 * c-basic-offset: 8
2169 * End:
2170 */